2016-08-18 10 views
0

私は以下のようなJSONデータを持っている::HttpPostリクエストを使用する前にJSONObjectにデータを追加する方法は?

workidsパラメータの
{ 

    "lastName":"sadfsdf", 
     "email":"[email protected]", 
     "gender":"male", 
     "workids":[0,0], 
     "roleId":102 
} 

私はのparamsのArrayListのを使用。

{ 
List<Integer> ll = new ArrayList<Integer>(); 
ll.add(100); 
ll.add(102); 
} 

私はこの

{ 

    JSONObject json = new JSONObject(); 
    json.put("lastName", "bbdddb"); 
    json.put("email","[email protected]"); 
    json.put("gender","male"); 
    json.put("secGameIds",ll); 
    json.put("roleId",secGame); 
} 

これは私が作ったポストリクエストだったようにJSONオブジェクトにのparamsを追加しました。

{ 

    HttpPost post = new HttpPost(uri); 
    post.setHeader("Content-type", "application/json"); 
    post.setEntity(new StringEntity(json.toString(), "UTF-8")); 
    DefaultHttpClient client = new DefaultHttpClient(); 
    HttpResponse httpresponse = client.execute(post); 
    } 

私が原因の代わりに整数のリストの文字列として送信し、このパラメータjson.toString()にこのプログラム{json.put("secGameIds",ll);}を実行してみてください。 要求を行うときにのみ、そのパラメータを整数リストとしてどのようにすることができますか?

ありがとうございました&よろしく。

+0

おそらくJSONArrayを使用して代わりにJsonObject –

+0

へのArrayListを追加する必要があります使用そして、 "Integer Listの代わりにStringとして送信する" json.toString()を明確にすることはできますか? "もちろん、それは整数リストではなくJSONオブジェクトを明示的にtoString-dという文字列として送信します –

答えて

0

jsonschema2pojo.orgサービスを使用して、GSONライブラリを使用してJSONとの間で解析できるJavaクラスを生成します。 JSONでは、すべてのデータはIntegerではなくStringとして送信され、受信時に解析されます。 ご覧のとおり、JSONに文字列があっても、POJOのリストはIntegerのリストです。 Gsonは文字列を整数に解析します。 あなたのクラスがどのように見えるウィル(それは良い習慣です):

-----------------------------------com.example.User.java----------------------------------- 

package com.example; 

import java.util.ArrayList; 
import java.util.List; 
import javax.annotation.Generated; 
import com.google.gson.annotations.Expose; 
import com.google.gson.annotations.SerializedName; 

@Generated("org.jsonschema2pojo") 
public class User { 

@SerializedName("lastName") 
@Expose 
private String lastName; 
@SerializedName("email") 
@Expose 
private String email; 
@SerializedName("gender") 
@Expose 
private String gender; 
@SerializedName("workids") 
@Expose 
private List<Integer> workids = new ArrayList<Integer>(); 
@SerializedName("roleId") 
@Expose 
private Integer roleId; 

/** 
* 
* @return 
* The lastName 
*/ 
public String getLastName() { 
return lastName; 
} 

/** 
* 
* @param lastName 
* The lastName 
*/ 
public void setLastName(String lastName) { 
this.lastName = lastName; 
} 

/** 
* 
* @return 
* The email 
*/ 
public String getEmail() { 
return email; 
} 

/** 
* 
* @param email 
* The email 
*/ 
public void setEmail(String email) { 
this.email = email; 
} 

/** 
* 
* @return 
* The gender 
*/ 
public String getGender() { 
return gender; 
} 

/** 
* 
* @param gender 
* The gender 
*/ 
public void setGender(String gender) { 
this.gender = gender; 
} 

/** 
* 
* @return 
* The workids 
*/ 
public List<Integer> getWorkids() { 
return workids; 
} 

/** 
* 
* @param workids 
* The workids 
*/ 
public void setWorkids(List<Integer> workids) { 
this.workids = workids; 
} 

/** 
* 
* @return 
* The roleId 
*/ 
public Integer getRoleId() { 
return roleId; 
} 

/** 
* 
* @param roleId 
* The roleId 
*/ 
public void setRoleId(Integer roleId) { 
this.roleId = roleId; 
} 

} 
+0

ありがとうございました... これらの変数をオブジェクトに追加するにはどうすればよいですか? ありがとうございました – Jaccs

+0

RESTにRetrofitライブラリを使用する必要があります。チュートリアルhttps://github.com/codepath/android_guides/wiki/Consuming-APIs-with-Retrofit –

0

は何を以下のようにしようとした場合:

private void executeNetworkOperation(String jsonData) throws UnsupportedEncodingException { 

    urlConnection = null; 

    try { 
     URL connectionUrl = new URL(url); 
     urlConnection = (HttpURLConnection) connectionUrl.openConnection(); 
     urlConnection.setChunkedStreamingMode(0); 
     urlConnection.setConnectTimeout(timeoutConnection); 
     urlConnection.setReadTimeout(timeoutRead); 
     urlConnection.setDoInput(true); 
     urlConnection.setDoOutput(true); 
     urlConnection.setRequestMethod("POST"); 
     urlConnection.setRequestProperty("Accept", "application/json"); 
     urlConnection.setRequestProperty("Content-type", "application/json"); 

     OutputStream outputStream = urlConnection.getOutputStream(); 
     outputStream.write(jsonData.getBytes()); 
     outputStream.flush(); 
     outputStream.close(); 


     InputStream is = urlConnection.getInputStream(); 

    } catch (MalformedURLException e){ 
     AppUtils.showLog(TAG, e.getMessage(), 2); 
    } catch (IOException e) { 
     AppUtils.showLog(TAG, e.getMessage(), 2); 
    } catch (Exception e) { 
     AppUtils.showLog(TAG, e.getMessage(), 2); 
    }finally { 
     if(null != urlConnection) { 
      urlConnection.disconnect(); 
     } 
    } 
} 
+0

を参照すると、jsonData変数が私のjsonデータです。 – Jaccs

+0

はい。私はこのようにして、その作業を完璧にしました。サインアップ手続きにjsonDataを使用しました。 –

関連する問題