gpt4 book ai didi

java - 使用volley库进行JSON解析

转载 作者:行者123 更新时间:2023-12-02 03:47:54 24 4
gpt4 key购买 nike

您好,任何有 volley 库经验的人都可以帮我在下面的代码中获取 JSON 吗?我收到错误日志:

D/Volley: [13] BasicNetwork.logSlowRequests: HTTP response for request=<[ ] http://digitaresolutions.com/apps/drugindex/getbrand.php?id=bactiflox 0xdf379d06 NORMAL 1> [lifetime=19209], [size=160], [rc=200], [retryCount=0]; I/Search_view: org.json.JSONException: Value of type java.lang.String cannot be converted to JSONObject

public class Search_view extends AppCompatActivity {
String brandURL = "http://digitaresolutions.com/apps/drugindex/getbrand.php?id=";

private String TAG = this.getClass().getSimpleName();
private ListView lstView;
private RequestQueue mRequestQueue;
private ArrayList<BrandModel> arrBrand ;
private LayoutInflater lf;
private VolleyAdapter va;
private ProgressDialog pd;

@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){

}
JsonObjectRequest jr = new JsonObjectRequest(Request.Method.GET,url,null,new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.i(TAG,response.toString());
parseJSON(response);
va.notifyDataSetChanged();
pd.dismiss();
; }
},new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.i(TAG,error.getMessage());
}
});
mRequestQueue.add(jr);



}

private void parseJSON(JSONObject json){
try{
JSONObject value = json.getJSONObject(null);
JSONArray items = value.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();
}


}


public class BrandModel
{
private String details;

private String manu;

private String drugbrand;

public String getDetails ()
{
return details;
}

public void setDetails (String details)
{
this.details = details;
}

public String getManu ()
{
return manu;
}

public void setManu (String manu)
{
this.manu = manu;
}

public String getDrugbrand ()
{
return drugbrand;
}

public void setDrugbrand (String drugbrand)
{
this.drugbrand = drugbrand;
}

@Override
public String toString()
{
return "ClassPojo [details = "+details+", manu = "+manu+", drugbrand = "+drugbrand+"]";
}
}


class VolleyAdapter extends BaseAdapter{

@Override
public int getCount() {
return arrBrand.size();
}

@Override
public Object getItem(int x) {
return arrBrand.get(x);
}

@Override
public long getItemId(int x) {
return 0;
}

@Override
public View getView(int x, View view, ViewGroup viewGroup) {
ViewHolder vh ;
if(view == null){
vh = new ViewHolder();
view = lf.inflate(R.layout.brand_row_listview,null);
vh.tvTitle = (TextView) view.findViewById(R.id.txtTitle);
vh.tvManu = (TextView) view.findViewById(R.id.txtManufacturer);
vh.tvDetails = (TextView) view.findViewById(R.id.txtDetails);
view.setTag(vh);
}
else{
vh = (ViewHolder) view.getTag();
}

BrandModel nm = arrBrand.get(x);
vh.tvTitle.setText(nm.getDrugbrand());
vh.tvManu.setText(nm.getManu());
vh.tvDetails.setText(nm.getDetails());
return view;
}

class ViewHolder{
TextView tvTitle;
TextView tvManu;
TextView tvDetails;

}
// End JSON Loading code block
}

}

最佳答案

看看这是否有帮助

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...");

}

}

关于java - 使用volley库进行JSON解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36151484/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com