2017-04-25 9 views
-1

アンドロイドアプリケーションでjsonを使用してサーバーにデータをアップロードする必要があります。 これは、コメントをアップロードするために提供される詳細です。jsonを使用してサーバーにデータをアップロードするandroid

------WebKitFormBoundaryZEhUiG82B2leVEc9 
Content-Disposition: form-data; name="Comment" 
Testing update 

これは私が更新に使用したコードです。

URL url = new URL("MY URL comes here"); // here is your URL path 
String js = "Content-Disposition: form-data; name=Comment"; 
String id = "ID=1"; //need to mention the id for upload 
String json = "testttting update"; 
JSONObject postDataParams = new JSONObject();  
HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 
conn.setRequestProperty("Cookie", cookie); 
conn.setReadTimeout(15000); 
conn.setConnectTimeout(15000); 
conn.setRequestMethod("POST"); 
conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded"); 
conn.setDoInput(true); 
conn.setDoOutput(true); 
conn.setUseCaches(false); 
OutputStream os = conn.getOutputStream(); 
os.write(js.getBytes()); 
os.write(id.getBytes()); 
os.write(json.getBytes()); 
os.flush(); 
os.close(); 

このリクエストに対する応答はありません。それだけがサーバーに更新されます。よろしくお願いします。私はたくさん試して、解決策を見つけられませんでした。

+0

を呼び出してみますか? –

+0

私は今までボレーを使っていませんでした。あなたがボレーを使用する場合、コードがどのように変化するのかコードを伝えることができますか? – paul

+0

'------ WebKitFormBoundaryZEhUiG82B2leVEc9'。はい。あなたの境界はどこですか? – greenapps

答えて

0

、これはどのようにボレーを使用することについてPOSTリクエスト

public class SendPostRequest extends AsyncTask<Void, Void, String> { 

     protected void onPreExecute(){} 

     protected String doInBackground(Void... arg0) { 

      try { 

       URL url = new URL("MY URL comes here"); // here is your URL path 

       JSONObject postDataParams = new JSONObject(); 
       postDataParams.put("ID", "1"); 
       postDataParams.put("Name", "Chetan"); 

       Log.e("params",postDataParams.toString()); 

       HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 
       conn.setReadTimeout(60000 /* milliseconds */); 
       conn.setConnectTimeout(60000 /* milliseconds */); 
       conn.setRequestMethod("POST"); 
       conn.setDoInput(true); 
       conn.setDoOutput(true); 

       OutputStream os = conn.getOutputStream(); 
       BufferedWriter writer = new BufferedWriter(
         new OutputStreamWriter(os, "UTF-8")); 
       writer.write(getPostDataString(postDataParams)); 

       writer.flush(); 
       writer.close(); 
       os.close(); 

       int responseCode=conn.getResponseCode(); 

       if (responseCode == HttpsURLConnection.HTTP_OK) { 

        BufferedReader in=new BufferedReader(new 
          InputStreamReader(
          conn.getInputStream())); 

        StringBuffer sb = new StringBuffer(""); 
        String line=""; 

        while((line = in.readLine()) != null) { 

         sb.append(line); 
         break; 
        } 

        in.close(); 
        return sb.toString(); 

       } 
       else { 
        return new String("false : "+responseCode); 
       } 
      } 
      catch(Exception e){ 
       return new String("Exception: " + e.getMessage()); 
      } 

     } 

     @Override 
     protected void onPostExecute(String result) { 
      /*In result you will get your response*/ 
      Toast.makeText(getApplicationContext(), result, 
        Toast.LENGTH_LONG).show(); 
     } 
    } 
    public String getPostDataString(JSONObject params) throws Exception { 

     StringBuilder result = new StringBuilder(); 
     boolean first = true; 

     Iterator<String> itr = params.keys(); 

     while(itr.hasNext()){ 

      String key= itr.next(); 
      Object value = params.get(key); 

      if (first) 
       first = false; 
      else 
       result.append("&"); 

      result.append(URLEncoder.encode(key, "UTF-8")); 
      result.append("="); 
      result.append(URLEncoder.encode(value.toString(), "UTF-8")); 

     } 
     return result.toString(); 
    } 
関連する問題