PHPサーバースクリプトに対してHTTPでPOSTを実行しています。
静的文字列を試してみると成功しますが、ユーザーが入力しているデータを取得する必要があります..
ユーザー入力からデータを取得しようとしたとき。
ソニーの電話: ランタイム例外
日食で:
Fatal Exception: AsycnTask #1 - java.lang.RunTimeException : An error occured while executing doInBackground()
at android.os.AsynkTask$3.FutureTask.finishCompletion(FutureTask.java:352)
クラスメイン:
case R.id.imageButtonServer:
{
textView = (TextView)findViewById(R.id.tvUserServerResponse);
new HttpPostDemo().execute(textView);
break;
}
クラス HttpPostDemo:
public class HttpPostDemo extends AsyncTask<TextView, Void, String>
{
TextView textView;
//Only present when trying to retrieve text from editText2 field//
EditText editText2;
@Override
protected String doInBackground(TextView... params)
{
this.textView = params[0];
BufferedReader inBuffer = null;
String url = "http://myserver.com/android_java.php";
String result = "fail";
//This fails//
String mail = editText2.getText().toString();
//This runs//
String mail = 'mail';
try {
HttpClient httpClient = new DefaultHttpClient();
HttpPost request = new HttpPost(url);
List<NameValuePair> postParameters = new ArrayList<NameValuePair>();
postParameters.add(new BasicNameValuePair("operanda", "5"));
postParameters.add(new BasicNameValuePair("operandb", "6"));
postParameters.add(new BasicNameValuePair("answer", "11"));
postParameters.add(new BasicNameValuePair("mail", mail));
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(postParameters);
main.xml :
<EditText
android:id="@+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Email Address"
android:inputType="textEmailAddress" />