【问题标题】:ListView using volleyListView 使用凌空
【发布时间】:2018-06-01 16:32:27
【问题描述】:

我正在尝试使用 json 将 sql server 连接到 android studio 这是我的代码。 我在 android studio 上没有任何错误,但是当我测试它时,我的手机上出现错误消息“连接错误”。 我该如何解决这个问题?

MainActivity

List<RowItem> rowItems=new ArrayList<RowItem>();
ListView mylistview;
private CustomAdapter adapter;
JSONArray peoples = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    subject();
}

public void subject(){
    String url = "http://myIP-Here/MSSQL/client.php";
    StringRequest stringRequest = new StringRequest(Request.Method.GET, url,new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {

                    Log.d("************", response);
                    Toast.makeText(MainActivity.this, response, Toast.LENGTH_SHORT).show();
                    Log.d("response",response);

                    try {

                        JSONObject jsonObj = new JSONObject(response);
                        peoples = jsonObj.getJSONArray("json");
                        mylistview = (ListView)findViewById(R.id.list);

                        adapter = new CustomAdapter(MainActivity.this, rowItems);

                        mylistview.setAdapter(adapter);
                        for(int i=0;i<peoples.length();i++) {
                            JSONObject c = peoples.getJSONObject(i);

                            RowItem goodsItems = new  RowItem();
                            goodsItems.setItem_id(c.getInt("item_id"));
                           goodsItems.setItem_name(c.getString("item_name"));



                            rowItems.add(goodsItems);

                            mylistview.setOnItemClickListener(MainActivity.this);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            },
            new Response.ErrorListener() {
                public void onErrorResponse(VolleyError error) {
                    Toast.makeText(MainActivity.this, "Error in connection", Toast.LENGTH_SHORT).show();
                    Log.i("response","error");
                }
            }){
        @Override
        protected Map<String, String> getParams() {
            Map<String, String> params = new HashMap<>();
            //params.put("UserEmail", get_email);
            return params;
        }
    };

    RequestQueue requestQueue = Volley.newRequestQueue(MainActivity.this);
    requestQueue.add(stringRequest);
}

自定义适配器

public class CustomAdapter extends BaseAdapter {

private Activity activity;
private LayoutInflater inflater;
private  List<RowItem> rowItems;
Typeface font;


public CustomAdapter(Activity activity, List<RowItem> rowItems) {
    this.activity = activity;
    this.rowItems =rowItems;
}

Context context;
public CustomAdapter(Context context){
    this.context=context;
}

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

@Override
public Object getItem(int location) {
    return rowItems.get(location);
}

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    if (inflater == null)
        inflater = (LayoutInflater) activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if (convertView == null)
        convertView = inflater.inflate(R.layout.list_item, null);

    TextView item_id = (TextView) convertView.findViewById(R.id.item_id);
    TextView item_name = (TextView) convertView.findViewById(R.id.item_name);
 //   ImageView image = (ImageView) convertView.findViewById(R.id.imView);

    final RowItem m = rowItems.get(position);

   // Picasso.with(activity)
     //       .load(m.get_image())
       //     .resize(300, 300)
         //   .into(image);
  item_id.setText(m.getItem_id());
    item_name.setText(m.getItem_name());
    return convertView;
}
public void clearAdapter()
{
    rowItems.clear();
    notifyDataSetChanged();
}

}

行项目

public class RowItem {
public int  item_id;
public String  item_name;
public int img;

public RowItem(){
   this.item_id=item_id;
    this.item_name=item_name;}
  public int   getItem_id() {return item_id;}
public void setItem_id(int item_id) { this.item_id=item_id; }

public String  getItem_name() {return item_name;}
public void setItem_name(String item_name) { this.item_name=item_name; }

我的 sql 数据库中也有照片,存储为 varbinary(MAX),我想显示在我的 listView 上,我真的需要你的帮助。

【问题讨论】:

    标签: android json android-studio android-volley


    【解决方案1】:

    试试这个!

    1. 在 LAN 网络中进行测试时,检查您的设备是否已连接到向服务器发出请求并在同一网络上的 Internet。

    2. 如果您连接在局域网中,请检查您是否禁用了服务器的防火墙。

    3. 检查您发送请求的 URL。

    【讨论】:

    • 我是android studio的新手,logcat没有帮助,我找不到任何错误:(
    • 别担心我们会找到解决方案,您是否编辑过代码? @EnasAK
    • 通过Logcat面板搜索查看Logcat,如果没有得到请回复
    • 我找到了,我在 logcat 中寻找什么?
    • 您需要搜索关键字response。你会在那里得到你的错误。
    猜你喜欢
    • 2016-09-03
    • 1970-01-01
    • 1970-01-01
    • 2017-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-22
    • 1970-01-01
    相关资源
    最近更新 更多