致命异常AsyncTask#2我做错了什么?

时间:2014-11-21 22:51:42

标签: java android

我有一个查询VIN的php / mysql查询,如果VIN在数据库中它返回" VIN已经存在"我在哪里弄错了:错误报告说Fatal Exception : AsyncTask #2(我的代码实际上比这更好,当我在启动Sell页面之前尝试重写这个以运行php checkVin时问题就开始了)

11-21 16:34:43.736: E/AndroidRuntime(725): FATAL EXCEPTION: AsyncTask #2
11-21 16:34:43.736: E/AndroidRuntime(725): java.lang.RuntimeException: An error occured while executing doInBackground()
11-21 16:34:43.736: E/AndroidRuntime(725):  at android.os.AsyncTask$3.done(AsyncTask.java:299)
11-21 16:34:43.736: E/AndroidRuntime(725):  at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
11-21 16:34:43.736: E/AndroidRuntime(725):  at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
11-21 16:34:43.736: E/AndroidRuntime(725):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
11-21 16:34:43.736: E/AndroidRuntime(725):  at java.util.concurrent.FutureTask.run(FutureTask.java:137)
11-21 16:34:43.736: E/AndroidRuntime(725):  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
11-21 16:34:43.736: E/AndroidRuntime(725):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
11-21 16:34:43.736: E/AndroidRuntime(725):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
11-21 16:34:43.736: E/AndroidRuntime(725):  at java.lang.Thread.run(Thread.java:856)
11-21 16:34:43.736: E/AndroidRuntime(725): Caused by: java.lang.IllegalArgumentException: Host name may not be null
11-21 16:34:43.736: E/AndroidRuntime(725):  at org.apache.http.HttpHost.<init>(HttpHost.java:83)
11-21 16:34:43.736: E/AndroidRuntime(725):  at org.apache.http.impl.client.AbstractHttpClient.determineTarget(AbstractHttpClient.java:497)
11-21 16:34:43.736: E/AndroidRuntime(725):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:626)
11-21 16:34:43.736: E/AndroidRuntime(725):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:616)
11-21 16:34:43.736: E/AndroidRuntime(725):  at com.mobile.donswholesale.Scan.getServerResopnse(Scan.java:272)
11-21 16:34:43.736: E/AndroidRuntime(725):  at com.mobile.donswholesale.Scan.access$1(Scan.java:260)
11-21 16:34:43.736: E/AndroidRuntime(725):  at com.mobile.donswholesale.Scan$4.doInBackground(Scan.java:240)
11-21 16:34:43.736: E/AndroidRuntime(725):  at com.mobile.donswholesale.Scan$4.doInBackground(Scan.java:1)
11-21 16:34:43.736: E/AndroidRuntime(725):  at android.os.AsyncTask$2.call(AsyncTask.java:287)
11-21 16:34:43.736: E/AndroidRuntime(725):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
11-21 16:34:43.736: E/AndroidRuntime(725):  ... 5 more

private void addSellButtonListener() {
    Button sell = (Button) findViewById(R.id.sell_button);
    sell.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            sendDatatoServer();
        }
    });
}
private String formatDataAsJASON() {
    JSONObject root = new JSONObject();
    try {
        root.put("User", userId.getText().toString());
        root.put("Pword", userPass.getText().toString());
        root.put("VIN", VINID.getText().toString());
        return root.toString();
    } catch (JSONException e) {
        Log.d("JWP", "Can't format JSON");
    }
    return null;
}
private void sendDatatoServer() {
    final String json = formatDataAsJASON();
    new AsyncTask<Void, Void, String>() {
        @Override
        protected String doInBackground(Void... params) {
            return getServerResopnse(json);
        }
        @Override
        protected void onPostExecute(String result) {
            if (result == "VIN already exists") {
                Toast.makeText(Scan.this,
                        getString(R.string.vin_exists), Toast.LENGTH_LONG)
                        .show();
                final Intent i = new Intent(Scan.this, Scan.class);
                startActivity(i);
            } else {
                StartSell();
            }
        }
    }.execute();
}
private String getServerResopnse(String json) {
    HttpPost post = new HttpPost("http://" + serverIp.getText().toString()
            + "/chekVIN.php");
    try {
        StringEntity entity = new StringEntity(json);
        post.setEntity(entity);
        post.setHeader("Content-type", "application/json");
        DefaultHttpClient client = new DefaultHttpClient();
        BasicResponseHandler handler = new BasicResponseHandler();
        String response = client.execute(post, handler);
        return response;
    } catch (UnsupportedEncodingException e) {
        Log.d("JWP", e.toString());

    } catch (ClientProtocolException e) {
        Log.d("JWP", e.toString());
    } catch (IOException e) {
        Log.d("JWP", e.toString());
    }
    return null;
}
private void StartSell() {
    final Intent i = new Intent(Scan.this, Sell.class);
    EditText editText = (EditText) findViewById(R.id.VIN);
    String text = editText.getText().toString();
    EditText editText2 = (EditText) findViewById(R.id.Make);
    String text2 = editText2.getText().toString();
    EditText editText3 = (EditText) findViewById(R.id.Model);
    String text3 = editText3.getText().toString();
    EditText editText4 = (EditText) findViewById(R.id.Color);
    String text4 = editText4.getText().toString();
    EditText editText5 = (EditText) findViewById(R.id.Year);
    String text5 = editText5.getText().toString();
    try {
        FileOutputStream fos = openFileOutput(VinHolder,
                Context.MODE_PRIVATE);
        fos.write(text.getBytes());
        fos.close();
        FileOutputStream fos2 = openFileOutput(MakeHolder,
                Context.MODE_PRIVATE);
        fos2.write(text2.getBytes());
        fos2.close();
        FileOutputStream fos3 = openFileOutput(ModelHolder,
                Context.MODE_PRIVATE);
        fos3.write(text3.getBytes());
        fos3.close();
        FileOutputStream fos4 = openFileOutput(ColorHolder,
                Context.MODE_PRIVATE);
        fos4.write(text4.getBytes());
        fos4.close();
        FileOutputStream fos5 = openFileOutput(YearHolder,
                Context.MODE_PRIVATE);
        fos5.write(text5.getBytes());
        fos5.close();
    }
    catch (Exception e) {
        Log.d("DEBUGTAG", "File Not Saved" + text);
        e.printStackTrace();
    }
    startActivity(i);
}

1 个答案:

答案 0 :(得分:0)

因为我使用来自应用程序另一页面的文本文件来保存手动输入的IP地址,所以我需要携带该信息以便HttpPost(“http://”+ serverIp.getText()。toString ()+“chechvin.php”);实际上会有ip地址。 添加后:

public static final String SERVERIP =“sinfo.txt”;

一切正常。