Android Studio 1.5.1中的JSON解析 [英] JSON parsing in Android Studio 1.5.1
问题描述
这是我的JSON解析器类
JSONParser.java
public class JSONParser {
JSONArray cArray ;
JSONObject jsonObj;
InputStream is = null;
String jsonStr =;
String urlString;
上下文上下文;
String id;
字符串名称,电子邮件,手机;
联系人联系人;
列表<联系人>联系人列表;
public JSONParser(){
// TODO自动生成的构造函数存根
}
public JSONParser(String url){
this .urlString = url;
}
public List<联系人> getString(){
contactList = new ArrayList<联系人> ();
尝试{
// **此块中的错误**
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(urlString);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch(UnsupportedEncodingException e){
e.printStackTrace();
} catch(ClientProtocolException e){
e.printStackTrace();
} catch(IOException e){
e.printStackTrace();
} catch(NullPointerException e){
Log.e(NULL DATA,cant fetch+ e.toString());
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(is,iso-8859-1),8);
StringBuilder sb = new StringBuilder();
String line = null;
while((line = reader.readLine())!= null){
sb.append(line +\\\
);
}
is.close();
jsonStr = sb.toString();
} catch(异常e){
Log.e(缓冲区错误,转换结果错误+ e.toString());
}
if(jsonStr!= null){
try {
cArray = new JSONArray(jsonStr);
JSONObject jsonObj = null;
//循环所有联系人
for(int i = 0; i jsonObj = cArray.getJSONObject(i);
id = jsonObj.getString(foodno);
name = jsonObj.getString(foodname);
email = jsonObj.getString(foodtype);
/ * JSONObject phoneNo = jsonObj.getJSONObject(phone);
mobile = phoneNo.getString(mobile); * /
contacts = new联系人(id,name,email);
//向联系人列表添加联系人
contactList.add(contacts);
}
} catch(JSONException e){
e.printStackTrace();
} catch(NullPointerException e){
e.printStackTrace();
}
} else {
Log.e(JSONParser,无法从url获取任何数据);
}
return contactList;
}
}
如何连接以获取我可以传递给BufferedReader的那个json字符串?
在 build.gradle中添加Gson依赖
编译'com.google.code.gson:gson:2.4'
然后为json响应创建POJO类。
只需复制你的json响应字符串进入下面的链接。
提供您的类名称和包名称,并下载该文件。
现在回到我们的应用程序代码。
演示demo = new Gson()。fromJson(JSON STRING,Demo 。类);
演示是我们的POJO类,我们从 json to pojo converter
I did JSON parsing on Eclipse ADT and it was running perfectly. But when used the same code on Android Studio it was showing error in DefaultHttpClient and every other word in that block.
Here is my JSON parser class
JSONParser.java
public class JSONParser {
JSONArray cArray;
JSONObject jsonObj;
InputStream is = null;
String jsonStr = "";
String urlString;
Context context;
String id;
String name, email, mobile;
Contacts contacts;
List < Contacts > contactList;
public JSONParser() {
// TODO Auto-generated constructor stub
}
public JSONParser(String url) {
this.urlString = url;
}
public List < Contacts > getString() {
contactList = new ArrayList < Contacts > ();
try {
// **Error in this block**
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(urlString);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (NullPointerException e) {
Log.e("NULL DATA", "cant fetch " + e.toString());
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
jsonStr = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
if (jsonStr != null) {
try {
cArray = new JSONArray(jsonStr);
JSONObject jsonObj = null;
// looping through All Contacts
for (int i = 0; i < cArray.length(); i++) {
jsonObj = cArray.getJSONObject(i);
id = jsonObj.getString("foodno");
name = jsonObj.getString("foodname");
email = jsonObj.getString("foodtype");
/* JSONObject phoneNo = jsonObj.getJSONObject("phone");
mobile = phoneNo.getString("mobile");*/
contacts = new Contacts(id, name, email);
// adding contact to contact list
contactList.add(contacts);
}
} catch (JSONException e) {
e.printStackTrace();
} catch (NullPointerException e) {
e.printStackTrace();
}
} else {
Log.e("JSONParser", "Couldn't get any data from the url");
}
return contactList;
}
}
Can anyone suggest how to connect to get that json string which i can pass to BufferedReader ?
add Gson dependency in build.gradle
compile 'com.google.code.gson:gson:2.4'
then create POJO class for json response.
for that just copy your json response string into below link.
give your class name and package name in righ side form and download that file.
now come back in our app code.
Demo demo = new Gson().fromJson("JSON STRING",Demo.class);
Demo is our POJO class which we have created from json to pojo converter
这篇关于Android Studio 1.5.1中的JSON解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!