2013-03-06 27 views
0

我需要解析如下的Android - 连接错误

http://192.168.1.91/projects/gprs/ 

给出。如果我在我的浏览器我得到一个像下面给出的回应打开此链接,

{"machine":[{"id":"3688","machine_code":"999999","di1":"0","di2":"1","di3":"0","di4":"1","di5":"0","di6":"1","di7":"0","di8":"1"}]} 

但在我的项目,logcat的显示连接被拒绝

我需要在textview中获取值。如何实现此目的?

的java的值编码在下面粘贴,

AndroidJSONParsingActivity.java

public class AndroidJSONParsingActivity extends ListActivity { 

// url to make request 
private static String url = "http://192.168.1.91/projects/gprs/"; 

// JSON Node names 
private static final String TAG_CONTACTS = "machine"; 
private static final String TAG_ID = "di1"; 
private static final String TAG_NAME = "di2"; 
private static final String TAG_EMAIL = "di3"; 
private static final String TAG_ADDRESS = "di4"; 
private static final String TAG_GENDER = "di5"; 
private static final String TAG_PHONE = "di5"; 
private static final String TAG_PHONE_MOBILE = "di7"; 
private static final String TAG_PHONE_HOME = "di8"; 
//private static final String TAG_PHONE_OFFICE = "office"; 

// contacts JSONArray 
JSONArray contacts = null; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

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

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

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

    try { 
     // Getting Array of Contacts 
     contacts = json.getJSONArray(TAG_CONTACTS); 

     // looping through All Contacts 
     for(int i = 0; i < contacts.length(); i++){ 
      JSONObject c = contacts.getJSONObject(i); 

      // Storing each json item in variable 
      String id = c.getString(TAG_ID); 
      String name = c.getString(TAG_NAME); 
      String email = c.getString(TAG_EMAIL); 
      String address = c.getString(TAG_ADDRESS); 
      String gender = c.getString(TAG_GENDER); 

      // Phone number is agin JSON Object 
      JSONObject phone = c.getJSONObject(TAG_PHONE); 
      String mobile = phone.getString(TAG_PHONE_MOBILE); 
      String home = phone.getString(TAG_PHONE_HOME); 
      String office = phone.getString(TAG_PHONE_HOME); 

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

      // adding each child node to HashMap key => value 
      map.put(TAG_ID, id); 
      map.put(TAG_NAME, name); 
      map.put(TAG_EMAIL, email); 
      map.put(TAG_PHONE_MOBILE, mobile); 

      // adding HashList to ArrayList 
      contactList.add(map); 
     } 
    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 


    /** 
    * Updating parsed JSON data into ListView 
    * */ 
    ListAdapter adapter = new SimpleAdapter(this, contactList, 
      R.layout.list_item, 
      new String[] { TAG_NAME, TAG_EMAIL, TAG_PHONE_MOBILE }, new int[] { 
        R.id.name, R.id.email, R.id.mobile }); 

    setListAdapter(adapter); 

    // selecting single ListView item 
    ListView lv = getListView(); 

    // Launching new screen on Selecting Single ListItem 
    lv.setOnItemClickListener(new OnItemClickListener() { 

     public void onItemClick(AdapterView<?> parent, View view, 
       int position, long id) { 
      // getting values from selected ListItem 
      String name = ((TextView) view.findViewById(R.id.name)).getText().toString(); 
      String cost = ((TextView) view.findViewById(R.id.email)).getText().toString(); 
      String description = ((TextView) view.findViewById(R.id.mobile)).getText().toString(); 

      // Starting new intent 
      Intent in = new Intent(getApplicationContext(), SingleMenuItemActivity.class); 
      in.putExtra(TAG_NAME, name); 
      in.putExtra(TAG_EMAIL, cost); 
      in.putExtra(TAG_PHONE_MOBILE, description); 
      startActivity(in); 

     } 
    }); 



} 

} 

JSONParser.java

public class JSONParser { 

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

// constructor 
public JSONParser() { 

} 

public JSONObject getJSONFromUrl(String url) { 

    // Making HTTP request 
    try { 
     // defaultHttpClient 
     DefaultHttpClient httpClient = new DefaultHttpClient(); 
     HttpPost httpPost = new HttpPost(url); 

     HttpResponse httpResponse = httpClient.execute(httpPost); 
     HttpEntity httpEntity = httpResponse.getEntity(); 
     is = httpEntity.getContent();   

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

    try { 
     BufferedReader reader = new BufferedReader(new InputStreamReader(
       is, "iso-8859-1"), 8); 
     StringBuilder sb = new StringBuilder(); 
     String line = null; 
     while ((line = reader.readLine()) != null) { 
      sb.append(line + "\n"); 
     } 
     is.close(); 
     json = sb.toString(); 
    } catch (Exception e) { 
     Log.e("Buffer Error", "Error converting result " + e.toString()); 
    } 

    // try parse the string to a JSON object 
    try { 
     jObj = new JSONObject(json); 
    } catch (JSONException e) { 
     Log.e("JSON Parser", "Error parsing data " + e.toString()); 
    } 

    // return JSON String 
    return jObj; 

} 

}

+0

你应该粘贴代码。 – dmnlk 2013-03-06 08:45:14

+0

您是否允许在清单文件中访问互联网? – 2013-03-06 08:45:17

+0

是的,我已经给了许可@Mayu Mayooresan – 2013-03-06 08:59:37

回答

0

使用下面的方法来解析你的回应:

public void parseHttpResponse(String response){ 
    JSONObject object; 
    try { 
     object = (JSONObject) new JSONTokener(response).nextValue(); 
     JSONArray jsonArray = object.getJSONArray("machine"); 

     for (int i = 0; i < jsonArray.length(); i++) { 
      JSONObject c = jsonArray.getJSONObject(i); 
      String id=c.getString("id"); 
      String machineCode=c.getString("machine_code"); 
      //so as parse other 
      Log.v(TAG, "id: "+id+" machine code: "+machineCode); 

     } 
    } catch (JSONException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
} 

用途:

parseHttpResponse("your_response_here");