1

我试图对数据库进行 json 调用。我创建了两个类来从服务器上的数据库中获取联系人。但是代码似乎不起作用。请帮助我。

我的 activity.java 文件如下:-

package com.example.library;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

public class JSONParser {


    static InputStream is = null;
    static JSONObject jObj = null;
    static String json = "";

    // constructor
    public JSONParser() {

    }

    public JSONObject getJSONFromUrl(String url) throws JSONException {
        JSONObject json = null;
       // Making HTTP request
       try {
           // defaultHttpClient
           DefaultHttpClient httpClient = new DefaultHttpClient();
           HttpGet httpGet = new HttpGet(url);

           HttpResponse httpResponse = httpClient.execute(httpGet);
           if (httpResponse != null && httpResponse.getStatusLine().getStatusCode() == 200) {
               HttpEntity httpEntity = httpResponse.getEntity();
               String retSrc = EntityUtils.toString(httpEntity); 
              // parsing JSON
               json = new JSONObject(retSrc); //Convert String to JSON Object
           }


       } catch (UnsupportedEncodingException e) {
           e.printStackTrace();
       } catch (ClientProtocolException e) {
           e.printStackTrace();
       } catch (IOException e) {
           e.printStackTrace();
       }


       // return JSON String
       return json;

   }
    }
   /* public void writeJSON() {
          JSONObject object = new JSONObject();
          try {
            object.put("name", "Rock");
            object.put("score", new Integer(200));
            object.put("current", new Double(152.32));
            object.put("nickname", "Programmer");
          } catch (JSONException e) {
            e.printStackTrace();
          }
          System.out.println(object);
        } 
*/

我的联系人活动如下:-

package com.example.library;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.os.Bundle;


public class ContactActivity extends Activity 
{

//url to make request
private static String url = "http://192.168.0.100:3000/users.json";

private static int users;

// JSON Node names
private static final String TAG_ID = "id";
private static final String TAG_FIRST = "first_name";
private static final String TAG_MIDDLE = "middle_name";
private static final String TAG_LAST = "last_name";
private static final String TAG_EMAIL = "email";
private static final String TAG_ADDRESS = "address";
private static final String TAG_MOBILE = "mobile";

private static final int TAG_USERS = users;



// users JSONArray

        @Override
    protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.tab_test1);

// Hashmap for ListView
ArrayList<HashMap<String, String>> userList = new ArrayList<HashMap<String, String>>();

// Creating JSON Parser instance
JSONParser jParser = new JSONParser();

// getting JSON string from URL
JSONArray json = jParser.getJSONFromUrl(url);

// Getting Array of Contacts

JSONArray users = null;
// looping through All Contacts
for (int i = 0; i < json.length(); i++) {
JSONObject c = json.optJSONObject(i);

// Storing each json item in variable

// creating new HashMap

HashMap<String, String> map = new HashMap<String, String>();

String id = null;
// adding each child node to HashMap key => value
map.put(TAG_ID,id);
String first = null;
map.put(TAG_FIRST, first);
String middle = null;
map.put(TAG_MIDDLE, middle);
String last = null;
map.put(TAG_LAST, last);
String address = null;
map.put(TAG_ADDRESS, address);
String mobile = null;
map.put(TAG_MOBILE, mobile);

String email = null;
map.put(TAG_EMAIL, email);

// adding HashList to ArrayList
userList.add(map);
}

}
}

调试器HttpResponse httpResponse = httpClient.execute(httpGet);在 activity.java 文件中停止,当我运行它时应用程序停止/崩溃

有人可以帮我解决这个问题吗

4

1 回答 1

0

不确定,但您应该在不同的线程上运行 http 操作

像这样:

 new Thread(new Runnable() {
                    @Override
                    public void run() {
                        JSON parser = new JSON(getJSONfromURL(url));
                    }
                }).start();
于 2013-05-02T05:57:26.637 回答