2015-06-20 120 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腳本。