2016-09-22 13 views
-1

POSTを使用して文字列をurlに送信する必要があります。 文字列は次のようになります。POSTを使用してHttpURLConnectionを使用してjsonデータを送信する

data={"cmd":"sign_in",....} 

のでdoInBackgroundに、私はこのコードを使用:

HttpURLConnection connection = null; 

DataOutputStream outputStream = null; 

String json = "{\"cmd\": \"sign_in\",..."; 

try{ 

URL url = new URL("https://...?data="); 

connection = (HttpURLConnection)url.openConnection(); 
connection.setDoInput(true); 
connection.setDoOutput(true); 
connection.setUseCaches(false); 
connection.setRequestProperty("Connection", "Keep-Alive"); 
connection.setRequestProperty("Content-Type","application/json"); 
connection.setRequestProperty("charset", "utf-8"); 
connection.connect(); 
OutputStream os = connection.getOutputStream(); 
OutputStreamWriter osw = new OutputStreamWriter(os, "UTF-8"); 
osw.write(json); 
osw.flush(); 
osw.close();  
} 

私は

int serverResponseCode = connection.getResponseCode(); 
String serverResponseMessage = connection.getResponseMessage(); 
if(serverResponseMessage.length()>0) 
return true; 

を使用して、私の応答をチェックしかし、私は何の応答も受信していません。応答文字列はJSON文字列でなければなりません。しかし、私はlength()を使用して、文字列が返されているかどうかを確認しました。 このコードは動作しますか?どんな助け?

+0

を試してみてください? – Piyush

+0

アンドロイド外で試したときにAPIが動作していますか? –

+0

Neeraj、APIが動作しています。 – Amit

答えて

0

あなたはこの

OkHttpClient client = TCY.getmInstance().getOkHttpClient(); 
     client.setConnectTimeout(120, TimeUnit.SECONDS); // connect timeout 
     client.setReadTimeout(120, TimeUnit.SECONDS); // socket timeout 
     // System.setProperty("http.keepAlive", "true"); 
     formBody.add("abc", abc); 
     formBody.add("username", username); 
     formBody.add("password", "password"); 

     RequestBody body = formBody.build(); 
     Request request = new Request.Builder() 
       .url(url) 
       .cacheControl(new CacheControl.Builder() 
         .maxStale(1, TimeUnit.DAYS) 
         .build()) 
       .post(body) 
       /* .header("Connection", " keep-alive")*/ 
       .build(); 
     hit_link = "url= " + url + " params= " + bodyToString(request); 

     try { 
      Response response = client.newCall(request).execute(); 
      if (!response.isSuccessful()) { 
       result = response.toString(); 
       if (result.equals("") || result.equals("null") || result.length() == 0) { 
        // CommonUtilities.showToast(context, "Something went wrong. Try later."); 
       } 
      } else { 

      } 
      result = response.body().string(); 
      /* Headers responseHeaders = response.headers(); 
      for (int i = 0; i < responseHeaders.size(); i++) { 
       Log.e("CallAddr", responseHeaders.name(i) + ": " + responseHeaders.value(i)); 
      }*/ 
     } catch (Exception ex) { 
      ex.printStackTrace(); 
     } 
     return result; 
+0

私は強制されるべきですHttpURLConnectionを使用する – Amit

0

ようFormEncodingBuilder使用することができますが_Json_としてデータを掲載しているあなたのコード内でそのため、この

 URL obj = new URL(this.url);//url assigned on constructor 
     HttpURLConnection httpURLConnection = (HttpURLConnection) obj.openConnection(); 
     httpURLConnection.setRequestMethod("POST"); 
     httpURLConnection.setRequestProperty("Content-Type", "application/json"); 
     httpURLConnection.setConnectTimeout(5000); 
     String urlParameters = this.headerData.toString();//headerdata is json and conveting is to string to send to server. 


     DataOutputStream wr = new DataOutputStream(httpURLConnection.getOutputStream()); 
     wr.writeBytes(urlParameters); 
     wr.flush(); 
     wr.close(); 


     int responseCode = httpURLConnection.getResponseCode();//getting response code 

     BufferedReader bufferedReader = null; 
     if (responseCode == 200) { 
      bufferedReader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream())); 
     } else { 
      bufferedReader = new BufferedReader(new InputStreamReader(httpURLConnection.getErrorStream())); 
     } 
関連する問題