任何有排球库的人都可以帮助我在下面的代码中获取JSON。我得到一个错误日志:
D / Volley:[13] BasicNetwork.logSlowRequests:对请求的HTTP响应=< [] http://digitaresolutions.com/apps/drugindex/getbrand.php?id=bactiflox 0xdf379d06正常1> [lifetime = 19209],[size = 160],[rc = 200],[retryCount = 0]; I / Search_view:org.json.JSONException:类型java.lang.String的值无法转换为JSONObject
Traceback (most recent call last):
File "C:\Python27\lib\site-packages\django\core\handlers\base.py", line 174, in get_response
response = self.process_exception_by_middleware(e, request)
File "C:\Python27\lib\site-packages\django\core\handlers\base.py", line 172, in get_response
response = response.render()
File "C:\Python27\lib\site-packages\django\template\response.py", line 160, in render
self.content = self.rendered_content
File "C:\Python27\lib\site-packages\django\template\response.py", line 137, in rendered_content
content = template.render(context, self._request)
File "C:\Python27\lib\site-packages\django\template\backends\django.py", line 95, in render
return self.template.render(context)
File "C:\Python27\lib\site-packages\django\template\base.py", line 206, in render
return self._render(context)
File "C:\Python27\lib\site-packages\django\template\base.py", line 197, in _render
return self.nodelist.render(context)
File "C:\Python27\lib\site-packages\django\template\base.py", line 992, in render
bit = node.render_annotated(context)
File "C:\Python27\lib\site-packages\django\template\base.py", line 959, in render_annotated
return self.render(context)
File "C:\Python27\lib\site-packages\django\template\loader_tags.py", line 173, in render
return compiled_parent._render(context)
File "C:\Python27\lib\site-packages\django\template\base.py", line 197, in _render
return self.nodelist.render(context)
File "C:\Python27\lib\site-packages\django\template\base.py", line 992, in render
bit = node.render_annotated(context)
File "C:\Python27\lib\site-packages\django\template\base.py", line 959, in render_annotated
return self.render(context)
File "C:\Python27\lib\site-packages\django\template\loader_tags.py", line 173, in render
return compiled_parent._render(context)
File "C:\Python27\lib\site-packages\django\template\base.py", line 197, in _render
return self.nodelist.render(context)
File "C:\Python27\lib\site-packages\django\template\base.py", line 992, in render
bit = node.render_annotated(context)
File "C:\Python27\lib\site-packages\django\template\base.py", line 959, in render_annotated
return self.render(context)
File "C:\Python27\lib\site-packages\django\template\loader_tags.py", line 69, in render
result = block.nodelist.render(context)
File "C:\Python27\lib\site-packages\django\template\base.py", line 992, in render
bit = node.render_annotated(context)
File "C:\Python27\lib\site-packages\django\template\base.py", line 959, in render_annotated
return self.render(context)
File "C:\Python27\lib\site-packages\django\template\loader_tags.py", line 69, in render
result = block.nodelist.render(context)
File "C:\Python27\lib\site-packages\django\template\base.py", line 992, in render
bit = node.render_annotated(context)
File "C:\Python27\lib\site-packages\django\template\base.py", line 959, in render_annotated
return self.render(context)
File "C:\Python27\lib\site-packages\django\template\library.py", line 223, in render
_dict = self.func(*resolved_args, **resolved_kwargs)
File "C:\Python27\lib\site-packages\django\contrib\admin\templatetags\admin_list.py", line 326, in result_list
'results': list(results(cl))}
File "C:\Python27\lib\site-packages\django\contrib\admin\templatetags\admin_list.py", line 302, in results
yield ResultList(None, items_for_result(cl, res, None))
File "C:\Python27\lib\site-packages\django\contrib\admin\templatetags\admin_list.py", line 293, in __init__
super(ResultList, self).__init__(*items)
File "C:\Python27\lib\site-packages\django\contrib\admin\templatetags\admin_list.py", line 202, in items_for_result
f, attr, value = lookup_field(field_name, result, cl.model_admin)
File "C:\Python27\lib\site-packages\django\contrib\admin\utils.py", line 290, in lookup_field
value = attr()
File "C:\Users\Uchechukwu\Desktop\DoreenProject\doreenProject\doreenselly\models.py", line 49, in item_remaining
return self.quantity - self.sold
TypeError: unsupported operand type(s) for -: 'int' and 'unicode'
[22/Mar/2016 11:36:49] "GET /admin/doreenselly/addinventory/ HTTP/1.1" 500 260601
}
答案 0 :(得分:1)
将JsonObjectRequest替换为StringRequest 即
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.brand_main);
lf = LayoutInflater.from(this);
arrBrand = new ArrayList<BrandModel>();
va = new VolleyAdapter();
lstView = (ListView) findViewById(R.id.listView);
lstView.setAdapter(va);
mRequestQueue = Volley.newRequestQueue(this);
// getting attached intent data
Intent i = getIntent();
String product = i.getStringExtra("product");
// displaying selected product name
String url = brandURL + product;
// JSON Data
pd = ProgressDialog.show(this,"Loading...","Please Wait...");
try{
Thread.sleep(2000);
}catch(Exception e){
}
StringRequest req=new StringRequest(url, new Listener<String>() {
@Override
public void onResponse(String response) {
// TODO Auto-generated method stub
try {
parseJSON(new JSONObject(response));
va.notifyDataSetChanged();
pd.dismiss();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}, new ErrorListener() {
@Override
public void onErrorResponse(VolleyError arg0) {
// TODO Auto-generated method stub
pd.dismiss();
}
});
mRequestQueue.add(req);
}
用以下代码替换parseJSON函数。
private void parseJSON(JSONObject json){
try{
if(!json.isNull("result")) {
JSONArray items = json.getJSONArray("result");
for (int x = 0; x < items.length(); x++) {
JSONObject item = items.getJSONObject(x);
BrandModel nm = new BrandModel();
nm.setDrugbrand(item.optString("drugbrand"));
nm.setManu(item.optString("manu"));
nm.setDetails(item.optString("details"));
arrBrand.add(nm);
}
}
}
catch(Exception e){
e.printStackTrace();
}
}
试试这个。它奏效了。
答案 1 :(得分:1)
See if this helps
public class SomeActivity extends AppCompatActivity {
ProgressDialog pd;
TextView txtView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_volley);
txtView = (TextView) findViewById(R.id.txt_volley);
String id = "bactiflox";
String url = new Uri.Builder()
.encodedPath("http://digitaresolutions.com")
.appendEncodedPath("apps/drugindex/getbrand.php")
.appendQueryParameter("id", id)
.build().toString();
JsonObjectRequest req = new JsonObjectRequest(url, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
pd.dismiss();
txtView.setText(response.toString());
try {
JSONArray mydata = response.getJSONArray("mydata");
for (int i = 0; i < mydata.length(); i++) {
JSONObject obj = mydata.getJSONObject(i);
Log.i("JSON", obj.getString("manu"));
Log.i("JSON", obj.getString("details"));
Log.i("JSON", obj.getString("drugbrand"));
}
} catch (JSONException e) {
txtView.setText(e.toString());
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
pd.dismiss();
txtView.setText(String.valueOf(error));
}
});
addToRequestQueue(req); // TODO: Implement
pd = ProgressDialog.show(this,"Loading...","Please Wait...");
}
}
答案 2 :(得分:0)
删除此行
JSONObject value = json.getJSONObject(null);
更改此行...
JSONArray items = value.getJSONArray("result")
到
JSONArray items = json.getJSONArray("result");