別のプログラムでgetInfoメソッドでコードブロックを実行しようとしましたが、エラーなしで実行されました。以前はこの問題は接続エラーのように見えましたが、修正後はこれらのエラーが発生しました。AsyncTaskでhttpgetリクエストを実行中にエラーが発生しました
クリックイベントに応答して、コードにlocalhost/getUser.phpを問い合わせて、結果のJSONオブジェクトをUser.javaに保存します。
04-07 09:36:55.641: E/AndroidRuntime(3724): FATAL EXCEPTION: AsyncTask #1
04-07 09:36:55.641: E/AndroidRuntime(3724): Process: com.example.meetup,PID: 3724
04-07 09:36:55.641: E/AndroidRuntime(3724): java.lang.RuntimeException: An error occured while executing doInBackground()
04-07 09:36:55.641: E/AndroidRuntime(3724): at android.os.AsyncTask$3.done(AsyncTask.java:300)
04-07 09:36:55.641: E/AndroidRuntime(3724): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
04-07 09:36:55.641: E/AndroidRuntime(3724): at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
04-07 09:36:55.641: E/AndroidRuntime(3724): at java.util.concurrent.FutureTask.run(FutureTask.java:242)
04-07 09:36:55.641: E/AndroidRuntime(3724): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
04-07 09:36:55.641: E/AndroidRuntime(3724): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
04-07 09:36:55.641: E/AndroidRuntime(3724): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
04-07 09:36:55.641: E/AndroidRuntime(3724): at java.lang.Thread.run(Thread.java:818)
04-07 09:36:55.641: E/AndroidRuntime(3724): Caused by: java.lang.IllegalArgumentException: Host name may not be null
04-07 09:36:55.641: E/AndroidRuntime(3724): at org.apache.http.HttpHost.<init>(HttpHost.java:83)
04-07 09:36:55.641: E/AndroidRuntime(3724): at org.apache.http.impl.client.AbstractHttpClient.determineTarget(AbstractHttpClient.java:497)
04-07 09:36:55.641: E/AndroidRuntime(3724): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
04-07 09:36:55.641: E/AndroidRuntime(3724): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
04-07 09:36:55.641: E/AndroidRuntime(3724): at com.example.meetup.User.getInfo(User.java:53)
04-07 09:36:55.641: E/AndroidRuntime(3724): at com.example.meetup.User.createStudent(User.java:36)
04-07 09:36:55.641: E/AndroidRuntime(3724): at com.example.meetup.MainActivity$HttpTask.doInBackground(MainActivity.java:39)
04-07 09:36:55.641: E/AndroidRuntime(3724): at com.example.meetup.MainActivity$HttpTask.doInBackground(MainActivity.java:1)
04-07 09:36:55.641: E/AndroidRuntime(3724): at android.os.AsyncTask$2.call(AsyncTask.java:288)
04-07 09:36:55.641: E/AndroidRuntime(3724): at java.util.concurrent.FutureTask.run(FutureTask.java:237)
04-07 09:36:55.641: E/AndroidRuntime(3724): ... 4 more
MainActivity.javaファイル
package com.example.meetup;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends Activity {
private EditText userField, passField;
private User user;
@Override
protected void onCreate(Bundle savedInstanceState)
{
user = new User();
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
userField = (EditText)findViewById(R.id.editText1);
passField = (EditText)findViewById(R.id.editText2);
}
protected void onStart()
{
super.onStart();
Log.i("Progress", "You made it to onStart");
}
public void signIn(View v)
{
Log.i("Progress", "You made it to signIn");
new HttpTask().execute();
}
private class HttpTask extends AsyncTask<Void,Boolean,Boolean>{
@Override
protected Boolean doInBackground(Void... params) {
return user.createStudent(userField.getText().toString(), passField.getText().toString());
}
protected void onPostExecute(Boolean result) {
Log.i("result",user.getName());
}
}
}
User.javaは
package com.example.meetup;
import java.io.IOException;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;
import android.net.Uri;
import android.util.Log;
public class User
{
static final String ID = "Id";
static final String USER = "Username";
static final String PASS = "Password";
static final String NAME = "Name";
private boolean userCreated;
private JSONObject User;
public User()
{
userCreated = false;
User = new JSONObject();
}
public boolean createStudent(String username, String password)
{
userCreated = getInfo(username, password);
return userCreated;
}
private boolean getInfo(String username, String password)
{
HttpResponse response;
Log.i("Progress", "You made it to getInfo");
try {
HttpClient client = new DefaultHttpClient();
Uri uri = Uri.parse("localhost:8080/getUser.php")
.buildUpon()
.appendQueryParameter("username", username)
.appendQueryParameter("password", password)
.build();
Log.i("Uri is", uri.toString());
HttpGet request = new HttpGet(uri.toString());
response = client.execute(request);
User = new JSONObject(new BasicResponseHandler().handleResponse(response));
}
catch (ClientProtocolException e) {
// TODO Auto-generated catch block
Log.e("Network error",e.toString());
return false;
} catch (IOException e) {
// TODO Auto-generated catch block
Log.e("Network error",e.toString());
return false;
} catch (JSONException e){
Log.e("Network error",e.toString());
return false;
}
return true;
}
public int getId()
{
if(userCreated)
{
try {
return User.getInt(ID);
} catch (JSONException e) {
e.printStackTrace();
}
}
return -1;
}
public String getName()
{
if(userCreated)
{
try {
return User.getString(NAME);
} catch (JSONException e) {
e.printStackTrace();
}
}
return null;
}
public String getUser()
{
if(userCreated)
{
try {
return User.getString(USER);
} catch (JSONException e) {
e.printStackTrace();
}
}
return null;
}
}