本文介绍了什么是org.json.JSONException的原因:网址没有价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是在code .ofcourse的网址传回的数据。
保护无效showList(){
尝试{
JSONObject的jsonObj =新的JSONObject(myJSON);
人民= jsonObj.getJSONArray(TAG_RESULTS); 的for(int i = 0; I< peoples.length();我++){
JSONObject的C = peoples.getJSONObject(I)
字符串ID = c.getString(TAG_ID);
字符串URL = c.getString(TAG_URL);
Listitem.add(新列表项(ID,URL));
} GridViewAdapter适配器=新GridViewAdapter(这一点,R.layout.grid_item_layout,列表项);
// gridView.setAdapter(gridAdapter); list.setAdapter(适配器); }赶上(JSONException E){
e.printStackTrace();
}
asycntask和网址
保护字符串doInBackground(字符串... PARAMS){
DefaultHttpClient的HttpClient =新DefaultHttpClient(新BasicHttpParams());
HttpPost httppost =新HttpPost(http://justedhak.comlu.com/get-data.php); //取决于你的Web服务
httppost.setHeader(内容类型,应用/ JSON); 为InputStream的InputStream = NULL;
字符串结果= NULL;
尝试{
HTT presponse响应= httpclient.execute(httppost);
HttpEntity实体= response.getEntity(); 的InputStream = entity.getContent();
// JSON默认为UTF-8
读者的BufferedReader =新的BufferedReader(新的InputStreamReader(InputStream中,UTF-8),8);
StringBuilder的SB =新的StringBuilder(); 串线= NULL;
而((行= reader.readLine())!= NULL)
{
sb.append(行+\\ n);
}
结果= sb.toString();
}赶上(例外五){
//哎呀
}
最后{
尝试{如果(InputStream的!= NULL)inputStream.close();}赶上(例外挤){}
}
返回结果;
}
解决方案
您的网址是,你没,T提到的任何JSON API从它数据u必须做这样的事情
...
this is the code .ofcourse the url is returning data.
protected void showList(){
try {
JSONObject jsonObj = new JSONObject(myJSON);
peoples = jsonObj.getJSONArray(TAG_RESULTS);
for(int i=0;i<peoples.length();i++){
JSONObject c = peoples.getJSONObject(i);
String id = c.getString(TAG_ID);
String url = c.getString(TAG_URL);
Listitem.add(new Listitem(id,url));
}
GridViewAdapter adapter = new GridViewAdapter(this, R.layout.grid_item_layout, Listitem);
// gridView.setAdapter(gridAdapter);
list.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
}
asycntask and url
protected String doInBackground(String... params) {
DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams());
HttpPost httppost = new HttpPost("http://justedhak.comlu.com/get-data.php");
// Depends on your web service
httppost.setHeader("Content-type", "application/json");
InputStream inputStream = null;
String result = null;
try {
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
inputStream = entity.getContent();
// json is UTF-8 by default
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
result = sb.toString();
} catch (Exception e) {
// Oops
}
finally {
try{if(inputStream != null)inputStream.close();}catch(Exception squish){}
}
return result;
}
解决方案
Your url is http://justedhak.comlu.com/get-data.php", you didn,t mentioned any json api to get data from it u must do something likehttp://justedhak.comlu.com/get-data.php/apiName?param1=...
这篇关于什么是org.json.JSONException的原因:网址没有价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!