2017-11-29 24 views
-2

私はVolleyが初めてです。私は、バリーコーディングを使用してAndroidアプリで廃止予定のHttpClientとHttpPostコーディングを変更しようとしています。 hereからJsonParsingを学び、自分のコードで実装することができました。 このコードは、サーバーからの応答を解析し、場合によってはjsonオブジェクトまたは配列を読み取る方法を示します。JSONオブジェクトをapiに投稿して、ボレーを使用してAndroidで未加工テキストを受け取る方法

Jsonオブジェクトをサーバーに送信してデータベースに保存し、生データを受信する方法を教えてもらえますか。ソリューションまたは同様のコードへのリンクさえも評価されます。私はたくさん検索しましたが、無駄なく

私は、次のコードをその等価なものに変更したいと思います。

private void saveToSite() { 
    try{ 
     // Create a new HttpClient and Post Header 
     HttpClient httpclient = new DefaultHttpClient(); 

     HttpPost httppost = new HttpPost("http://clctn.mysite.com/data/checklog.php"); 
     JSONObject json = new JSONObject(); 

     json.put("name", ed_name.getText().toString()); 
     json.put("address", ed_address.getText().toString()); 
     json.put("contact", ed_contact.getText().toString()); 

     JSONArray postjson=new JSONArray(); 
     postjson.put(json); 

     // Post the data: 
     httppost.setHeader("json",json.toString()); 
     httppost.getParams().setParameter("jsonpost",postjson); 

     // Execute HTTP Post Request 
     HttpResponse response = httpclient.execute(httppost); 

     // for JSON: 
     if(response != null) 
     { 
      InputStream is = response.getEntity().getContent(); 
      BufferedReader reader = new BufferedReader(new InputStreamReader(is)); 
      StringBuilder sb = new StringBuilder(); 

      String line = null; 
      try { 
       while ((line = reader.readLine()) != null) { 
        sb.append(line + "\n"); 
       } 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } finally { 
       try { 
        is.close(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
      } 
     } 
    }catch (ClientProtocolException e) { 
     Log.e("cpe1 ", e.toString()); 
    } catch (IOException e) { 
     Log.e("ioe1 ", e.toString()); 
    } catch (JSONException e){ 
     Log.e("jsonex ", e.toString()); 
    } 
} 
+0

[これ](https://developer.android.com/training/volley/index.html)で通過します。 – ADM

答えて

0
StringRequest request = new StringRequest(Request.Method.POST, 
StaticVeriable.GET_PHOTOS, new Response.Listener<String>() { 
     @Override 
     public void onResponse(String response) { 

     } 
    }) { 
     @Override 
     protected Map<String, String> getParams() throws AuthFailureError { 
      HashMap<String, String> params = new HashMap<>(); 
      params.put("parameter1", ""+yourjsonObject.toString();); 

      return params; 
     } 
    }; 
    RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext()); 
    requestQueue.add(request); 
} 
関連する問題