我一直在寻找各地,以便了解为什么我的函数onPostExecute永远不会被调用,所以我没有得到我的结果字符串和应用程序粉碎! 这是AsyncTask的代码:
public class HttpAsync extends AsyncTask<Void,Void,String> {
private String Json;
private int response;
private String auth;
private String name;
private AsyncResponse delegate=null;
public HttpAsync(AsyncResponse response, String Json){
this.Json=Json;
this.delegate=response;
}
@Override
protected String doInBackground(Void... params) {
URL url;
try {
url = new URL("http://192.168.0.141:8080/ProgettoProva/AndroidApp");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setRequestProperty("Content-Type", "application/json, charset=UTF-8");
conn.setRequestProperty("Accept", "text/json");
conn.setFixedLengthStreamingMode(Json.getBytes().length);
OutputStream writer = conn.getOutputStream();
writer.write(Json.getBytes("UTF-8"));
writer.close();
response=conn.getResponseCode();
if(response==HttpURLConnection.HTTP_OK) {
auth = "authorized";
BufferedReader read =new BufferedReader(new InputStreamReader (conn.getInputStream()));
StringBuffer buf= new StringBuffer();
String line;
while((line=read.readLine())!=null){
buf.append(line);
}
read.close();
GsonBuilder builder=new GsonBuilder();
builder.registerTypeAdapter(String.class,new DataDeserializer());
Gson gson=builder.create();
name = gson.fromJson(buf.toString(), String.class);
}
else if(response==HttpURLConnection.HTTP_UNAUTHORIZED) {
auth = "not authorized";
}
else if(response==HttpURLConnection.HTTP_ACCEPTED) {
auth = "registrato";
}
else if(response==HttpURLConnection.HTTP_CONFLICT){
auth="gia presente";
}
conn.disconnect();
} catch (IOException e) {
Log.e("ERRORE", e.getMessage());
}
Log.d("STRINGA INVIATA: ", name+"/"+auth);
return name+"/"+auth;
}
@Override
protected void onPostExecute(String result){
Log.d("ONPOSTEXECUTED:", result);
delegate.returnFinish(result);
}
这是main_activity的代码部分,我执行asynctask:
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
email = textEmail.getText().toString();
password = textPass.getText().toString();
name=textName.getText().toString();
if(value==1) {
if (email.equals("") || password.equals("")) {
Toast.makeText(getApplicationContext(), "campi vuoti", Toast.LENGTH_LONG).show();
return;
}
}else if(value==2){
if (email.equals("") || password.equals("") || name.equals("")) {
Toast.makeText(getApplicationContext(), "campi vuoti", Toast.LENGTH_LONG).show();
return;
}
}
finalJson = createJfile();
// Toast.makeText(getApplicationContext(), "Json:" + finalJson, Toast.LENGTH_LONG).show();
connection= new HttpAsync(Log_in.this,finalJson);
connection.execute();
get=auth.split("/");
getname=get[0];
auth=get[1];
日志中有一个例外:
01-13 10:57:32.829 31566-31566/com.example.stage.drawlayoutproject E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.stage.drawlayoutproject, PID: 31566
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String[] java.lang.String.split(java.lang.String)' on a null object reference
at com.example.stage.drawlayoutproject.Log_in$1.onClick(Log_in.java:87)
at android.view.View.performClick(View.java:5254)
at android.view.View$PerformClick.run(View.java:21174)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6862)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
实际上它只是在doInBackground的末尾打印日志而不打印onPostExecute中的日志
答案 0 :(得分:0)
问题在于:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.stage.drawlayoutproject, PID: 31566
java.lang.NullPointerException: Attempt to invoke virtual method
java.lang.String[] java.lang.String.split(java.lang.String)' on a null object reference
at com.example.stage.drawlayoutproject.Log_in$1.onClick(Log_in.java:87)
at android.view.View.performClick(View.java:5254
at android.view.View$PerformClick.run(View.java:21174)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6862)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
看起来像这样的连接&#39;是AsyncTask
的一个实例。不是HttpAsync
。
在这种情况下,您应该在声明此字段时将connection
的类型更改为HttpAsync
或填充connection
的通用参数。