Advertisement
Picko

JsonParser

Mar 11th, 2013
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.06 KB | None | 0 0
  1. public class JsonParser extends ListActivity {
  2.  
  3. JSONArray jArray;
  4. String result = null;
  5. InputStream is = null;
  6. StringBuilder sb=null;
  7. private ProgressDialog dialog;
  8.  
  9.  
  10.  
  11. @Override
  12. public void onCreate(Bundle savedInstanceState) {
  13. super.onCreate(savedInstanceState);
  14. //setContentView(R.layout.login_activity);
  15.  
  16. /*output = (TextView) findViewById(R.id.output);
  17. txt = (EditText) findViewById(R.id.txt);
  18. get = (Button) findViewById(R.id.get_button);
  19. post = (Button) findViewById(R.id.post_button);*/
  20.  
  21.  ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
  22. //http post
  23. try{
  24.      HttpClient httpclient = new DefaultHttpClient();
  25.      HttpPost httppost = new HttpPost("http://192.168.1.2:8080/test/jsp/Android/Select.jsp");
  26.      httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
  27.      HttpResponse response = httpclient.execute(httppost);
  28.      HttpEntity entity = response.getEntity();
  29.      is = entity.getContent();
  30.      }catch(Exception e){
  31.          Log.e("log_tag", "Error in http connection"+e.toString());
  32.     }
  33. //convert response to string
  34. try{
  35.       BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
  36.        sb = new StringBuilder();
  37.        sb.append(reader.readLine() + "\n");
  38.  
  39.        String line="0";
  40.        while ((line = reader.readLine()) != null) {
  41.                       sb.append(line + "\n");
  42.         }
  43.         is.close();
  44.         result=sb.toString();
  45.         }catch(Exception e){
  46.               Log.e("log_tag", "Error converting result "+e.toString());
  47.         }
  48. //paring data
  49. int ct_id;
  50. String ct_name;
  51. try{
  52.       jArray = new JSONArray(result);
  53.       JSONObject json_data=null;
  54.       for(int i=0;i<jArray.length();i++){
  55.              json_data = jArray.getJSONObject(i);
  56.              ct_id=json_data.getInt("id");
  57.              ct_name=json_data.getString("user");
  58.          }
  59.       }
  60.       catch(JSONException e1){
  61.           Toast.makeText(getBaseContext(), "No City Found" ,Toast.LENGTH_LONG).show();
  62.       } catch (ParseException e1) {
  63.             e1.printStackTrace();
  64.     }
  65. }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement