在AsyncTask上运行httpget请求时出错

时间:2016-04-07 14:56:02

标签: java android apache android-asynctask

我尝试在单独的程序中运行getInfo方法中的代码块,并且运行时没有错误。之前的问题似乎是连接错误,但在修复后出现了这些错误。

我希望代码查询localhost / getUser.php并将结果JSON对象保存在User.java中以响应click事件。

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;
}
}

2 个答案:

答案 0 :(得分:0)

请看一下:

  

引起:java.lang.IllegalArgumentException:主机名可能不为null

这就是AddWithValue()被解雇的原因。您是否尝试在Uri,http或https?上设置协议,具体取决于您使用的是什么。

答案 1 :(得分:0)

我使用了排球,似乎工作正常