2017-12-18 150 views
0

我想發送文本值到服務器,這是名稱,電話,位置,備註,但每次每個值都丟失 第一次名稱不會,下次撥打電話和下一個位置。 ..我的JSONObject發送空值到服務器

更新:

Jsoncode:

public static String POST(String url, Persoenter code heren person){ 
    InputStream inputStream = null; 
    String result = ""; 
    try { 

     HttpClient httpclient = new DefaultHttpClient(); 
     HttpPost httpPost = new HttpPost(url); 

     String json = ""; 
     JSONObject jsonObject = new JSONObject(); 
     jsonObject.accumulate("name",person.getName()); 
     jsonObject.accumulate("phone", person.getPhone()); 
     jsonObject.accumulate("remarrks",person.getRemarks()); 
     jsonObject.accumulate("credated_dt", person.getCreatedat()); 

     jsonObject.accumulate("emp_code", person.getCreatedby()); 

     jsonObject.accumulate("location", person.getLocation()); 
     jsonObject.accumulate("add_fld1", "Test"); 
     jsonObject.accumulate("add_fld2", "Test"); 
     jsonObject.accumulate("add_fld3", "Test"); 




     json = jsonObject.toString(); 
     StringEntity se = new StringEntity(json); 
     Log.e("sent",""+json); 
     httpPost.setEntity(se); 
     httpPost.setHeader("Accept", "application/json"); 
     httpPost.setHeader("Content-type", "application/json"); 

     HttpResponse httpResponse = httpclient.execute(httpPost); 

     inputStream = httpResponse.getEntity().getContent(); 
     if(inputStream != null) 
      result = convertInputStreamToString(inputStream); 
     else 
      result = "Did not work!"; 
    } catch (Exception e) { 
     Log.d("InputStream", e.getLocalizedMessage()); 
    } 
    return result; 
} 

Httppost

private class sync extends AsyncTask<String, String, String> { 


    InputStream inputStream = null; 
    OutputStream outputStream = null; 

    protected String doInBackground(String... urls) { 

     person = new Person(); 

     person.setName(eName.getText().toString()); 
     person.setPhone(ePhonenumber.getText().toString()); 
     person.setLocation(eLocation.getText().toString()); 
     person.setRemarks(eRemarks.getText().toString()); 
     person.setCreatedby(eCreatedby.getText().toString()); 
     person.setCreatedat(eCreatedat.getText().toString()); 
     return POST(urls[0],person); 
    } 
@Override 
    protected void onPreExecute() { 
     progress.setTitle("Connecting to server"); 
     progress.setMessage("Sending Data.... Please wait!"); 
     progress.setCanceledOnTouchOutside(false); 
     progress.show(); 
    } 

    @Override 
    protected void onPostExecute(String s) { 
       progress.dismiss(); 
       Intent Homeintent = getIntent(); 
Homeintent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION); 
finish(); 
startActivity(Homeintent); 

    } 

輸出 1:{ 「名稱」: 「beta33」, 「電話」: 「33」, 「remarrks」: 「33」, 「credated_dt」:「2017年12月20日11 :56:32「,」emp_code「:」[email protected]「,」location「:」33「} 2:{」name「:」beta33「,」phone「:」33「,」remarrks「: 「33」,「credated_dt」:「2017-12-20 11:56:43」,「emp_code」:「[email protected]」,「location」:「33」} 3:{「name」:「 「,」電話「:」34「,」remarrks「:」34「,」credated_dt「:」2017-12-20 11:56:52「,」emp_code「:」[email protected]「,」位置「 :「34」}

在嘗試了一兩次嘗試之後,名字不會出現,我不知道爲什麼......請別人幫我解決這個問題

+2

我建議你使用一個網絡庫,如排球或改造來處理這些類型的操作。這種程序已過時,並且隨着項目擴展而變得難以維護。 – KBJ

+0

是的,我想使用凌空..但截至目前這是必需的 – Flash

+0

在調試模式下檢查'JSONObject joj = getonline();'的值。你是否獲得預期的價值。 –

回答

-2

我發佈了一個工作代碼示例,將其替換爲此代碼,然後查找這裏出現了什麼錯誤。

try { 
      URL url = new URL("http://-------"); 
      HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection(); 
      httpURLConnection.setDoOutput(true); 
      httpURLConnection.setRequestMethod("POST"); 
      httpURLConnection.setRequestProperty("Content-Type", "application/json"); 
      httpURLConnection.connect(); 
      DataOutputStream wr = new DataOutputStream(httpURLConnection.getOutputStream()); 
      wr.write(inputJson.getBytes()); //your input json in string format 
      wr.flush(); 
      BufferedReader br = new BufferedReader(new InputStreamReader(
        (httpURLConnection.getInputStream()))); 
      StringBuffer bfr = new StringBuffer(); 
      String output = ""; 
      while ((output = br.readLine()) != null) { 
       bfr.append(output); 
      } 
      String serverPostRes = bfr.toString(); 
      wr.close(); 
      httpURLConnection.disconnect(); 
      bfr.delete(0,bfr.length()); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     return serverPostRes; 
    } 

在你的問題中inputJson你可以通過

String inputJson = "{ " 
      + "  \"add_fld1\": \"" + ("Test"+ i) + "\"," 
      + "  \"add_fld2\": \"" + ("Test"+ i+1) + "\"," 
      + "  \"add_fld3\": \"" + ("Test"+ i+2) + "\"," 
      + "  \"credated_dt\": \"" + formattedDate + "\"," 
      + "  \"emp_code\": \"" + eCreatedby.getText().toString() + "\"," 
      + "  \"location\": \"" + eLocation.getText().toString() + "\"," 
      + "  \"name\": \"" + eName.getText().toString() + "\"," 
      + "  \"phone\": \"" + ePhonenumber.getText().toString() + "\"," 
      + "  \"remarrks\": \"" + eRemarks.getText().toString() + "\"," 
      + "}"; 
+0

問題不是與連接..你的代碼和我的唯一區別是你使用http後,我使用URL連接..我的URL連接只是一個工作代碼 – Flash

+1

這兩個代碼都使用「POST」方法。這個答案不包含將值傳遞給服務器端,我認爲這是主要問題。 – KBJ

+0

我也使用相同的'HttpURLConnection'。請再次檢查。問題在於發送請求並處理響應。 – Shambhu