2017-05-08 15 views
-1

我在我的android應用程序中想出了一個奇怪的錯誤,我想要做一個POST請求來將數據存儲在MySQL數據庫中。POST請求不能在線程上運行Android

但問題是,應用程序終止時,我嘗試在一個線程中執行此操作,並且錯誤中沒有指示,因此很難修復此問題。

的代碼波紋管:

thread = new Thread(new Runnable() { 
       @Override 
       public void run() { 
        try { 
         android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_BACKGROUND); 
         URL url = new URL("myurl"); 
         HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 
         connection.setDoOutput(true); 
         connection.setRequestMethod("POST"); 
         connection.setRequestProperty("Content-type","application/json"); 

         JSONObject jsobj = new JSONObject(); 
         jsobj.put("pseudo",edit.getText().toString()); 
         jsobj.put("score",score); 

         DataOutputStream wr = new DataOutputStream(connection.getOutputStream()); 
         wr.writeBytes(jsobj.toString()); 
         wr.flush(); 
         wr.close(); 

        } catch (IOException e) { 
         e.printStackTrace(); 
        }catch (JSONException je){ 
         je.printStackTrace(); 
        } 
       } 
      }); 
      thread.start(); 

非常感謝你的幫助,你會得到我:)。

+0

把你的錯誤日誌這裏 –

+0

看來,我需要在設備上的互聯網連接,所以我要告訴他需要互聯網 –

+0

當然的應用程序,您需要互聯網許可。 –

回答

0

試試這個

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

     String pseudo =""; 
     String score=""; 
     public CallWebService(String pseudo,String score){ 
      this.pseudo = pseudo; 
      this.score = score; 
     } 
     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 

     } 

     @Override 
     protected String doInBackground(String... params) { 
      try { 
       android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_BACKGROUND); 
       URL url = new URL("myurl"); 
       HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 
       connection.setDoOutput(true); 
       connection.setRequestMethod("POST"); 
       connection.setRequestProperty("Content-type","application/json"); 

       JSONObject jsobj = new JSONObject(); 
       jsobj.put("pseudo",pseudo); 
       jsobj.put("score",score); 

       DataOutputStream wr = new DataOutputStream(connection.getOutputStream()); 
       wr.writeBytes(jsobj.toString()); 
       wr.flush(); 
       wr.close(); 

      } catch (IOException e) { 
       e.printStackTrace(); 
      }catch (JSONException je){ 
       je.printStackTrace(); 
      } 
      return null; 
     } 

     @Override 
     protected void onPostExecute(String s) { 
      super.onPostExecute(s); 
     } 
    } 

,並添加下面的代碼執行上面的AsyncTask

`new CallWebService(edt.getText().toString(),score).execute();` 
+0

廢話。你不看到OP使用線程了嗎? – greenapps

0

首先,對於這種使用情況下,我建議你使用AsyncTask因爲 這是一個簡單的請求和AsyncTasks更容易使用。

其次,您只是將請求發送到服務器,但沒有得到響應,因此如果請求在服務器端失敗或者發出錯誤的請求,您將不會意識到它。您可以使用okhttp來簡化過程。

+0

這完全沒有答案。請使用意見的建議。 – greenapps

0

在setContentView()之前添加此行;

StrictMode.ThreadPolicy policy = new 
StrictMode.ThreadPolicy.Builder().permitAll().build(); 
StrictMode.setThreadPolicy(policy); 
setContentView(); 
+0

非常糟糕的建議。並不需要使用線程。 – greenapps

0

謝謝你們,我已經找到了問題的答案,我們需要增加網絡權限的程序的工作。因此,我們必須對你的清單中添加Internet權限是這樣的:

<uses-permission android:name="android.permission.INTERNET"/>