2016-11-18 120 views
0

我試圖使用okhttp3庫發佈post請求,但我不斷收到內部錯誤。我是使用這個庫和android studio的新手,但是我所看到的每種方法都有這樣的post請求。我是否做了錯誤的請求,如果有的話是否有修復它?json和okhttp3的HTTP post請求

package com.example.john.okhttp; 


import android.os.AsyncTask; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 
import org.json.JSONObject; 
import okhttp3.MediaType; 
import okhttp3.OkHttpClient; 
import okhttp3.Request; 
import okhttp3.RequestBody; 
import okhttp3.Response; 


public class MainActivity extends AppCompatActivity { 
    private Button btnSendHttpRequest; 
    private EditText etJsonResponse; 
    private TextView View; 
    private TextView View2; 
    private OkHttpClient okHttpClient; 
    private Request request ; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     //set button and text view values 
     btnSendHttpRequest = (Button) findViewById(R.id.btnSendRequest); 
     View = (TextView) findViewById(R.id.view1) ; 
     View2 = (TextView) findViewById(R.id.textView4); 
     etJsonResponse = (EditText) findViewById(R.id.etjson); 
     //response for button 
     btnSendHttpRequest.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       //http request 
       new Requesting().execute(); 
      } 
     }); 

    } 


    public class Requesting extends AsyncTask<String , Void , String> 
    { 
     // post request stuff 
     @Override 
     protected String doInBackground(String... params) { 
      MediaType JSON = MediaType.parse("application/json; charset=utf-8"); 
      // get client 
      OkHttpClient client = new OkHttpClient(); 
      //post method to post to server 
      String url = "http://45.55.92.243/newuser"; 
      String json = "{ 'id' : 333333, 'name' : john }"; 


      RequestBody body = RequestBody.create(JSON, json); 
      Request request = new Request.Builder() 
        .url(url) 
        .post(body) 
        .build(); 
      try(Response response = client.newCall(request).execute()){ 
       return response.body().string(); 
      } 
      catch(Exception e){ 
       System.out.println("section1"); 
       return null; 
      } 

    } 


    @Override 
    protected void onPostExecute(String retstr) { 
     super.onPostExecute(retstr); 
     //rewrite text view 
     try { 
      // create json ob from response 
      JSONObject jsonObj = new JSONObject(retstr); 
      //get the values from the json key value pairs 
      String id = jsonObj.toString(); 
      //update the text views 
      TextView textView = (TextView) findViewById(R.id.view1); 
      textView.setText(id); 
      System.out.println(retstr); 
     } 
     catch (Exception e){ 
      System.out.println(retstr); 
      System.out.println("section2"); 
     } 
     } 
    } 

} 

回答

-1

在我看來,你應該閱讀okhttp3的演示,你不必與okhttp3使用的AsyncTask,這些庫中,線程將被創建!你只需要註冊一個回調聽的結果網絡!

+0

我確實計劃採取這一措施,但您認爲這就是我遇到此問題的原因嗎?我剛剛發現我的服務器正在收到請求,但json軟件包已空了。 –

+0

哦,請使用TextUtils(在android.text的包中)來確保服務器的結果不是空的! – SevenNight

+1

爲什麼不使用Volley Library?它可以爲你處理所有事情,而且速度也非常快。一探究竟。 – Rajeev