我正在使用AsynTask将数据发送到php并收到要打印的名称,但它显示以下错误。我该如何解决这个问题?
logcat的
12-29 21:38:27.588: W/dalvikvm(1283): threadid=11: thread exiting with uncaught exception (group=0x40a71930)
12-29 21:38:27.748: E/AndroidRuntime(1283): FATAL EXCEPTION: AsyncTask #1
12-29 21:38:27.748: E/AndroidRuntime(1283): java.lang.RuntimeException: An error occured while executing doInBackground()
12-29 21:38:27.748: E/AndroidRuntime(1283): at android.os.AsyncTask$3.done(AsyncTask.java:299)
12-29 21:38:27.748: E/AndroidRuntime(1283): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
12-29 21:38:27.748: E/AndroidRuntime(1283): at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
12-29 21:38:27.748: E/AndroidRuntime(1283): at java.util.concurrent.FutureTask.run(FutureTask.java:239)
12-29 21:38:27.748: E/AndroidRuntime(1283): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
12-29 21:38:27.748: E/AndroidRuntime(1283): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
12-29 21:38:27.748: E/AndroidRuntime(1283): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
12-29 21:38:27.748: E/AndroidRuntime(1283): at java.lang.Thread.run(Thread.java:856)
12-29 21:38:27.748: E/AndroidRuntime(1283): Caused by: java.lang.NullPointerException
12-29 21:38:27.748: E/AndroidRuntime(1283): at com.example.myweb.toPHP.getUserLoggedIn(toPHP.java:99)
12-29 21:38:27.748: E/AndroidRuntime(1283): at com.example.myweb.toPHP.doInBackground(toPHP.java:67)
12-29 21:38:27.748: E/AndroidRuntime(1283): at com.example.myweb.toPHP.doInBackground(toPHP.java:1)
12-29 21:38:27.748: E/AndroidRuntime(1283): at android.os.AsyncTask$2.call(AsyncTask.java:287)
12-29 21:38:27.748: E/AndroidRuntime(1283): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
12-29 21:38:27.748: E/AndroidRuntime(1283): ... 4 more
这是我向Java发送数据的Java代码:
toPHP.java
package com.example.myweb;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;
import org.json.JSONObject;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
public class toPHP extends AsyncTask{
final MainActivity main;
public toPHP(MainActivity main) {
this.main = main;
}
private JSONParser jsonParser;
String email,password;
EditText emailBox;
EditText passwordBox;
@Override
protected void onPreExecute() {
super.onPreExecute();
TextView textV1 = (TextView)main.findViewById(R.id.textV1);
textV1.setText("Reaching em!!");
}
@Override
protected JSONObject doInBackground(Object... v) {
//main = (MainActivity)parameters[0];
main.afterEffect("sending...");
emailBox = (EditText) main.findViewById(R.id.email);
passwordBox = (EditText) main.findViewById(R.id.password);
email = emailBox.getText().toString();
password = passwordBox.getText().toString();
JSONObject json = null;
main.afterEffect("sending2...");
try {
json = getUserLoggedIn(email, password);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace(); //9b6a45925f32c3ce5a8b3413605b16d6
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return json;
}
public JSONObject getUserLoggedIn(String email,String password) throws ClientProtocolException, IOException, JSONException{
JSONObject json = null;
/*
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("http://localhost/testand.php");
*/
List<NameValuePair> pairs = new ArrayList<NameValuePair>();
pairs.add(new BasicNameValuePair("email", email));
pairs.add(new BasicNameValuePair("password", password));
//post.setEntity(new UrlEncodedFormEntity(pairs));
//HttpResponse response = client.execute(post);
//HttpEntity resEntity = response.getEntity();
//if (resEntity != null) {
//String responseStr = EntityUtils.toString(resEntity).trim();
json = jsonParser.getJSONFromUrl("http://localhost/testand.php", pairs);
//}
return json;
}
protected void onPostExecute(JSONObject json) {
String myName="";
try {
myName = json.getString("name");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String str = myName + ", Welcome to Socionet. :) ";
main.afterEffect(str);
}
}
以下是我的主要Java代码:
MainActivity.java
package com.example.myweb;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;
import org.json.JSONObject;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
public class MainActivity extends Activity {
Button button;
EditText emailBox;
EditText passwordBox;
String emailId;
String passwordId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.login1);
emailBox = (EditText)findViewById(R.id.email);
passwordBox = (EditText)findViewById(R.id.password);
button.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
try {
new toPHP(MainActivity.this).execute();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
public void afterEffect(String str){
TextView textV1 = (TextView)findViewById(R.id.textV1);
textV1.setText(str);
}
}
以下是我的JSON Parser代码:
JSONParser.java
package com.example.myweb;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;
import android.util.Log;
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
// constructor
public JSONParser() {
}
public JSONObject getJSONFromUrl(String url, List params) {
// Making HTTP request
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "n");
}
is.close();
json = sb.toString();
Log.e("JSON", json);
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
}
编辑:这是我在savchak修复后的LOGCAT2响应:
LOGCAT2
12-30 06:44:19.664: W/System.err(777): org.apache.http.conn.HttpHostConnectException: Connection to http://localhost refused
12-30 06:44:19.744: W/System.err(777): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:183)
12-30 06:44:19.744: W/System.err(777): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
12-30 06:44:19.744: W/System.err(777): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
12-30 06:44:19.744: W/System.err(777): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
12-30 06:44:19.754: W/System.err(777): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
12-30 06:44:19.764: W/System.err(777): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
12-30 06:44:19.764: W/System.err(777): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
12-30 06:44:19.764: W/System.err(777): at com.example.myweb.JSONParser.getJSONFromUrl(JSONParser.java:33)
12-30 06:44:19.774: W/System.err(777): at com.example.myweb.toPHP.getUserLoggedIn(toPHP.java:99)
12-30 06:44:19.784: W/System.err(777): at com.example.myweb.toPHP.doInBackground(toPHP.java:67)
12-30 06:44:19.784: W/System.err(777): at com.example.myweb.toPHP.doInBackground(toPHP.java:1)
12-30 06:44:19.804: W/System.err(777): at android.os.AsyncTask$2.call(AsyncTask.java:287)
12-30 06:44:19.804: W/System.err(777): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
12-30 06:44:19.804: W/System.err(777): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
12-30 06:44:19.814: W/System.err(777): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
12-30 06:44:19.824: W/System.err(777): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
12-30 06:44:19.824: W/System.err(777): at java.lang.Thread.run(Thread.java:856)
12-30 06:44:19.824: W/System.err(777): Caused by: java.net.ConnectException: failed to connect to /127.0.0.1 (port 80): connect failed: ECONNREFUSED (Connection refused)
12-30 06:44:19.844: W/System.err(777): at libcore.io.IoBridge.connect(IoBridge.java:114)
12-30 06:44:19.864: W/System.err(777): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
12-30 06:44:19.864: W/System.err(777): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
12-30 06:44:19.864: W/System.err(777): at java.net.Socket.connect(Socket.java:842)
12-30 06:44:19.864: W/System.err(777): at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
12-30 06:44:19.874: W/System.err(777): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
12-30 06:44:19.884: W/System.err(777): ... 16 more
12-30 06:44:19.934: W/System.err(777): Caused by: libcore.io.ErrnoException: connect failed: ECONNREFUSED (Connection refused)
12-30 06:44:20.004: W/System.err(777): at libcore.io.Posix.connect(Native Method)
12-30 06:44:20.004: W/System.err(777): at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:85)
12-30 06:44:20.004: W/System.err(777): at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
12-30 06:44:20.025: W/System.err(777): at libcore.io.IoBridge.connect(IoBridge.java:112)
12-30 06:44:20.025: W/System.err(777): ... 21 more
12-30 06:44:20.025: E/Buffer Error(777): Error converting result java.lang.NullPointerException: lock == null
12-30 06:44:20.034: E/JSON Parser(777): Error parsing data org.json.JSONException: End of input at character 0 of
答案 0 :(得分:0)
您的问题是jsonParser
始终为null,因此在doInBackground
期间抛出NullPointerException。
答案 1 :(得分:0)
您的jsonParser字段为空引用。你应该初始化一些对象。
答案 2 :(得分:0)
我做的事情
JSONParser jsonParser = new JSONParser(); //in toPHP.java
http//localhost/testand.php
至http//myipadress/testand.php
allow all from