2017-09-16 10 views
-2

私は、Androidアプリケーションから、このPOSTリクエストを作成し、JSONレスポンスを取得したい:のJava andoirdポスト要求への再書き込みPHPのポスト要求(および応答を取得)

function send_post_to_url($url,$post) { 
    $ch = curl_init($url); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
    curl_setopt($ch,CURLOPT_POST, 1); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post)); 
    $return = curl_exec($ch); 
    curl_close($ch); 
    return $return; 
} 

$data['api_key'] = YOUR_API_KEY; 
$data['action'] = 'TestFunction'; 
$response = send_post_to_url('https://api.superget.co.il/',$data); 
echo $response; 

はどうすればこれを行うことができますか?

私は例外にtrowing続け以下が、接続しようとしている

 HttpURLConnection urlConnection; 
     urlConnection = (HttpURLConnection) ((new URL("https://api.superget.co.il").openConnection())); 
     urlConnection.setDoOutput(true); 
     urlConnection.setDoInput(true); 
     urlConnection.setRequestProperty("Content-Type", "application/json"); 
     urlConnection.setRequestProperty("Accept", "application/json"); 
     urlConnection.setRequestMethod("POST"); 
     urlConnection.connect(); 
+0

はアンドロイド例えば任意のライブラリhttp://square.github.io/okhttp/ HTTPSを試してみなければなりません。 //raw.githubusercontent.com/square/okhttp/master/samples/guide/src/main/java/okhttp3/guide/PostExample.java –

答えて

0

私のコード試してみてください。使用

public boolean sendPost(Map<String, Object> params, String urlAddress) { 

     URL url; 
     HttpURLConnection urlConnection = null; 

     try { 

      StringBuilder postData = new StringBuilder(); 
      for (Map.Entry<String, Object> param : params.entrySet()) { 
       if (postData.length() != 0) postData.append('&'); 
       postData.append(URLEncoder.encode(param.getKey(), "UTF-8")); 
       postData.append('='); 
       postData.append(URLEncoder.encode(String.valueOf(param.getValue()), "UTF-8")); 
      } 
      byte[] postDataBytes = postData.toString().getBytes("UTF-8"); 

      url = new URL(urlAddress); 
      urlConnection = (HttpURLConnection) url.openConnection(); 


      //urlConnection.setReadTimeout(10000 /* milliseconds */); 
      //urlConnection.setConnectTimeout(15000 /* milliseconds */); 

      //add reuqest header 
      urlConnection.setRequestMethod("POST"); 
      urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 
      urlConnection.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length)); 
      urlConnection.setRequestProperty("charset", "utf-8"); 

      //urlConnection.setUseCaches(false); 
      //urlConnection.setInstanceFollowRedirects(false); 


      urlConnection.setDoOutput(true); 
      // Send post request 
      DataOutputStream wr = new DataOutputStream(urlConnection.getOutputStream()); 
      wr.write(postDataBytes); //wr.writeBytes(postData.toString()); 
      wr.flush(); 
      wr.close(); 
      //urlConnection.getOutputStream().write(postDataBytes); 

      /* 
      int responseCode = urlConnection.getResponseCode(); 
      Log.e("ddd", "\nSending 'POST' request to URL : " + url); 
      Log.e("ddd", "Post parameters : " + postData.toString()); 
      Log.e("ddd", "Response Code : " + responseCode); */ 


      String response = ""; 
      BufferedReader in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(), "UTF-8")); 
      String inputLine; 
      while ((inputLine = in.readLine()) != null) 
       response += inputLine; 
      in.close(); 

      //get response 
      return response.trim().equals("ok"); 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } finally { 
      if (urlConnection != null) 
       urlConnection.disconnect(); 
     } 

     return false; 
    } 

Map<String, Object> params = new HashMap<>(); 
params.put("param1", txtContactNf.getText().toString()); 
params.put("param2", txtContactNf.getText().toString()); 
final boolean successSend = nu.sendPost(params, "http://yoursite.com/api.php"); 

sendPost方法をこの共同もしPHPスクリプトがokと答えると、trueブール結果が得られます。簡単に変更してjsonの結果を得ることができます。

編集:JSONの結果を得る

代わりreturn response.trim().equals("ok");

String response = ""; 
BufferedReader in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(), "UTF-8")); 
String inputLine; 
while ((inputLine = in.readLine()) != null) 
response += inputLine; 
in.close(); 

if(response.equals("no")) return null; 

return new JSONObject(response); 

戻り値の型は、JSONObject

+0

「アクション」を使って地図インスタンスを初期化した後は、私にとってはうまくいかない彼は私の行動を認識できないというサーバからの応答 – AndoridBegginer

+0

@AndoridBegginer応答結果とは何ですか?トーストやログ '応答'と私に言う。 –

関連する問題