2017-09-11 7 views
0

私はアンドロイドプログラミングでは新しいです。私はアンドロイドのネイティブにjsonを投稿する方法について質問したい。これは私が投稿したいと思っているjsonのサンプルです。生のタイプのJsonのアンドロイドの投稿

{ 
    "user_id":"88880005", 
    "keyword":"13828538", 
    "items":["99999999999999999996","99999999999999999997"] 
} 

これは私が行ったことです。

private void save() { 
    String URL = URL_Manager.URL_ScanIn; 
    Map<String, String> map = new HashMap<>(); 

    map.put("user_id", id); 
    map.put("keyword", keyword); 
    map.put("items", databarcode); 

    ProgressDialog progressDialog = new ProgressDialog(getActivity()); 
    progressDialog.setMessage("Loading"); 
    progressDialog.setCancelable(false); 
    try { 
     aQuery.progress(progressDialog).ajax(URL, map, String.class, new AjaxCallback<String>() { 
      @Override 
      public void callback(String url, String object, AjaxStatus status) { 
       try { 
        JSONObject jsonObject = new JSONObject(object); 
        String result = jsonObject.getString("success"); 
        String message= jsonObject.getString("message"); 
        Log.d("tag", jsonObject.toString()); 
       } catch (JSONException e) { 
        Toast.makeText(getActivity(), e.getMessage, Toast.LENGTH_LONG).show(); 
       } 
      } 
     }); 
    } catch (Exception e) { Toast.makeText(getActivity(), e.getMessage, Toast.LENGTH_LONG).show();    
    } 
} 

このサンプルでは、​​生のフォームデータではない郵便番号を使用しています。私のアンドロイドからそのデータを投稿するにはどうすればよいですか?

ありがとうございます。

答えて

0

Androidで次の手順を実行します。ありがとうございコード

 String[] items= new String[]{"99999999999","9999997456"}; 
     JSONObject object=new JSONObject(); 
     object.put("user_id",user_id); 
     object.put("keyword",keyword); 
     try { 
      JSONArray jsonArray = new JSONArray(Arrays.asList(items)); 
      object.put("items",jsonArray); 

     }catch (Exception e){} 
     String yourData = object.toString(); 
     StringEntity entity = null; 
     try { 
      entity = new StringEntity(yourData); 
      entity.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json")); 
     } catch(Exception e) { 
//Exception 
     } 

     String url="Your url here"; 

     new AsyncHttpClient().post(null,url,entity,"application/json", new AsyncHttpResponseHandler() { 
      @Override 
      public void onSuccess(int statusCode, cz.msebera.android.httpclient.Header[] headers, byte[] responseBody) { 
       try { 
        String object= new String(responseBody); 
        JSONObject jsonObject = new JSONObject(object); 
        String result = jsonObject.getString("success"); 
        String message= jsonObject.getString("message"); 
        Log.d("tag", jsonObject.toString()); 
       } catch (JSONException e) { 
        Toast.makeText(getActivity(), e.getMessage, Toast.LENGTH_LONG).show(); 
       } 
      } 

      @Override 
      public void onFailure(int statusCode, cz.msebera.android.httpclient.Header[] headers, byte[] responseBody, Throwable error) { 

      } 
     }); 

    } 
+0

ありがとうございます。私はこのコードを試してみます。 –

+0

私はそのコードで自分のメソッドコードを変更するだけですか?ありがとうございました。 –

0

JSONリクエストのHTTP POSTのサンプルコードです。 AsyncTaskまたはBackgroundスレッドの内部でこれを使用します。

import java.io.BufferedWriter; 
import java.io.InputStream; 
import java.io.OutputStream; 
import java.io.OutputStreamWriter; 
import java.net.HttpURLConnection; 
import java.net.URL; 

import javax.net.ssl.HttpsURLConnection; 


public String executePost() { 

    String requestJSON = "your json"; 
    InputStream inputStream = null; 
    String result = ""; 
    try { 
     String myurl = "put your serverURL" 
     URL url = new URL(myurl); 
     HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 
     conn.setConnectTimeout(NetworkHelper.NetworkConnectTimeOut); 
     conn.setReadTimeout(12000); 
     conn.setRequestMethod("POST"); 
     conn.setDoInput(true); 

     OutputStream os = conn.getOutputStream(); 
     BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8")); 
     writer.write(requestJSON); 
     writer.flush(); 
     writer.close(); 
     os.close(); 

     conn.connect(); 
     int responseCode = conn.getResponseCode(); 

     boolean responseStatus = false; 
     if (responseCode == HttpsURLConnection.HTTP_OK) { 
      responseStatus = true; 
     } 

     inputStream = conn.getInputStream(); 
     if (inputStream != null && responseStatus) { 
      result = readStream(inputStream); 
      status = getJSONResponseStatus(result); 
     } else { 
      result = "Did not work!"; 
     } 
    } catch (Exception e) { 
     Log.d(TAG, e.getMessage(), e); 
    } 
    Log.d("result****", result); 
    return result; 
} 
+0

の下に使用してWebサービスを呼び出す

compile 'com.loopj.android:android-async-http:1.4.9' 

あなたのGradleに依存関係を追加します。私はそのコードを試みます。 –