我有一个WCF
Webservice
,其中发送了一个数据模型,我通过JSon
(通过实体框架)在Android
中获得了这个数据模型,无论如何,我都可以通过此代码成功地获得JSON
,并将所有JSON
对象存储在AsyncTas
类的JSONArray
中,以及:
public class Consume extends AsyncTask<Void, Void, Void> {
InputStream inputStream = null;
String result = "";
private ArrayList<Contact> contacts = new ArrayList<Contact>();
@Override
protected Void doInBackground(Void... params) {
String URL = "http://x.x.x.x/MyWCF/Service1.svc/rest/getContact";
ArrayList<NameValuePair> param = new ArrayList<NameValuePair>();
try {
HttpClient httpClient = new DefaultHttpClient();
HttpPost post = new HttpPost(URL);
post.setEntity(new UrlEncodedFormEntity(param));
HttpResponse httpResponse = httpClient.execute(post);
HttpEntity httpEntity = httpResponse.getEntity();
//post.setHeader("content-type", "application/json");
inputStream = httpEntity.getContent();
} catch (UnsupportedEncodingException e1) {
Log.e("UnsupportedEncoding", e1.toString());
e1.printStackTrace();
} catch (ClientProtocolException e2) {
Log.e("ClientProtocolException", e2.toString());
e2.printStackTrace();
} catch (IllegalStateException e3) {
Log.e("IllegalStateException", e3.toString());
e3.printStackTrace();
} catch (IOException e4) {
Log.e("IOException", e4.toString());
e4.printStackTrace();
}
try {
BufferedReader bReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
StringBuilder sBuilder = new StringBuilder();
String line = null;
while ((line = bReader.readLine()) != null) {
sBuilder.append(line + "\n");
}
inputStream.close();
result = sBuilder.toString();
} catch (Exception e) {
Log.e("StringBuilding", "Error converting result " + e.toString());
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
try {
JSONObject object = new JSONObject(result);
JSONArray jArray = object.getJSONArray("getContactResult"); //here i create the JsonArray of all JsonObjects
//Here is the solutions, We make a list of out Contact and make it as down
List<Contact> contacts;
Type listType = new TypeToken<List<Contact>>() {
}.getType();
contacts= new Gson().fromJson(String.valueOf(jArray), listType);
//And here solution is ended !
} catch (JSONException e) {
e.printStackTrace();
}
}
我在android
中创建了一个联系人class
,代码如下:
public class Contact {
@SerializedName("name")
private String name;
@SerializedName("lastName")
private String lastName;
@SerializedName("phoneNumber")
private String phoneNumber;
@SerializedName("latitude")
private String latitude;
@SerializedName("longitude")
private String longitude;
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getLastName() {
return lastName;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setLatitude(String latitude) {
this.latitude = latitude;
}
public String getLatitude() {
return latitude;
}
public void setLongitude(String longitude) {
this.longitude = longitude;
}
public String getLongitude() {
return longitude;
}
}
我用老方法解析这个JSONArray
!用这个方法:
ArrayList<Contact> setFields(JSONArray jsonArray) {
ArrayList<Contact> contacts = new ArrayList<Contact>();
for(int i=0; i<jsonArray.length(); i++) {
try {
Contact contact = new Contact();
JSONObject object = (JSONObject) jsonArray.get(i);
contact.setName(object.getString("name"));
contact.setLastName(object.getString("lastName"));
contact.setPhoneNumber(object.getString("phoneNumber"));
contact.setLatitude(object.getString("latitude"));
contact.setLongitude(object.getString("longitude"));
contacts.add(contact);
} catch (JSONException e) {
e.printStackTrace();
}
}
return contacts;
}
它工作,但我不想处理和解析JSONArray
的这种老方法,并希望使用GSON
代替,任何人都可以帮助我与这个例子?这里是我的JSONArray
和JSON
对象:
{
"getContactResult": [
{
"id": 2041,
"lastName": "xxxx",
"latitude": xxx,
"longitude": xxx,
"name": "xxxx",
"phoneNumber": "xxxx"
}
]
}
谢谢
4条答案
按热度按时间7xzttuei1#
确保您的模型类与json参数和数据类型具有相同的名称。它会将jsonarray解析为java的类型List
qlzsbp2j2#
这已经回答了,但我想分享一件事给你。简单和最好的方法
有一个插件Gson为android工作室。你需要安装。然后去CTRL +插入。你可以创建gson文件。输入一些名称为java文件。
点击该文件,然后粘贴json数据。点击确定。你可以看到你创建的json到gson格式。
谢谢,希望这对你有帮助。
xdnvmnnf3#
Kotlin溶液
Java解决方案
5us2dqdw4#
将Gson和JSON结合起来(一种不同的方法),对于新手来说很简单。