2015-06-20 60 views
0

我觉得我提供的代码是我最好的选择。发送HTTP发布请求崩溃Android程序

主要活动

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

    } 

P在Mainifest文件ostData()方法

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

我已经启用了互联网接入。

许多类/对象上都有删除线(即 - 已弃用)。

为什么会这样?

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

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

任何想法?

+0

使用AppCompat活动而不是ActionBarActivity。这不是解决方案。只是一个建议。由于谷歌终于决定使用AppCompatActivity并且弃用ActionBarActivity。 –

+0

这是否适用于所有版本的android? –

+2

请发布崩溃日志 –

回答

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脚本。