发送HTTP Post请求会导致Android程序崩溃

时间:2015-06-20 16:56:05

标签: android http post

我觉得为我提供代码是我最好的选择。

主要活动

public class MainActivity extends ActionBarActivity {

        Button submitbtn;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            submitbtn = (Button) findViewById(R.id.button);
            submitbtn.setOnClickListener(new View.OnClickListener() {

                public void onClick(View v) {

                    PostDataAsyncTask runner = new PostDataAsyncTask();
                    runner.execute();
                }
            });
        }

主要活动中的其他方法和类别

PostDataAsyncTask (省略了未使用的覆盖方法)

    public class PostDataAsyncTask extends AsyncTask<String, String, String> {

        @Override
        protected String doInBackground(String... strings) {

            postData();
            return null;
        }

    }

PostData()方法

    private void postData(){
        try{
            String postReceiverUrl = "staffappfeedback.comule.com/insert-dp.php";

            HttpClient httpClient = new DefaultHttpClient();

            HttpPost httpPost = new HttpPost(postReceiverUrl);

            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3);
            nameValuePairs.add(new BasicNameValuePair("name", "Mike"));
            nameValuePairs.add(new BasicNameValuePair("summary", "My suggestion"));
            nameValuePairs.add(new BasicNameValuePair("description", "This is what I think you should do"));

            httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

            HttpResponse response = httpClient.execute(httpPost);
            HttpEntity resEntity = response.getEntity();

            if (resEntity != null) {

                String responseStr = EntityUtils.toString(resEntity).trim();

            }

        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

我已在Mainifest文件中启用了互联网访问。

许多类/对象都有删除线(即不推荐使用)。

为什么会出现这种情况?

这是我的程序崩溃的原因吗?

我的Android设备有调试问题所以我无法轻松找到问题,因此Stack Overflow上的帖子,但不要误解我,我花了很多时间试图找到问题。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

我将postData()更改为

private void postData(){
    try{
        String postReceiverUrl = "http://staffappfeedback.comule.com/insert-dp.php";

        HttpParams httpParameters = new BasicHttpParams();
        HttpClient httpClient = new DefaultHttpClient(httpParameters);

        httpClient.getParams().setParameter("http.protocol.version", HttpVersion.HTTP_1_1);
        httpClient.getParams().setParameter("http.socket.timeout", 2000);
        httpClient.getParams().setParameter("http.protocol.content-charset", HTTP.UTF_8);
        httpParameters.setBooleanParameter("http.protocol.expect-continue", false);

        HttpPost httpPost = new HttpPost(postReceiverUrl);
        httpPost.getParams().setParameter("http.socket.timeout", 5000);

        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        nameValuePairs.add(new BasicNameValuePair("name", "Mike"));
        nameValuePairs.add(new BasicNameValuePair("summary", "My suggestion"));
        nameValuePairs.add(new BasicNameValuePair("description", "This is what I think you should do"));

        UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8);
        httpPost.setEntity(formEntity);

        HttpResponse response = httpClient.execute(httpPost);

        in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
        StringBuffer sb = new StringBuffer("");
        String line = "";
        while ((line = in.readLine()) != null) {
            sb.append(line);
        }
        in.close();
        String result = sb.toString();

        Log.e("result", result);


        //HttpEntity resEntity = response.getEntity();


        //if (resEntity != null) {

            //String responseStr = EntityUtils.toString(resEntity).trim();

        //}

    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

来自服务器的日志响应

06-21 14:34:59.325: E/result(3794): Error: INSERT INTO comments VALUES     (NULL, android, android test, this is an android test)<br>You have an error in     your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'test, this is an android test)' at line 1

问题是除了更改行字符串postReceiverUrl =“http://staffappfeedback.comule.com/insert-dp.php”;使用PHP脚本。