Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class JsonParser extends ListActivity {
- JSONArray jArray;
- String result = null;
- InputStream is = null;
- StringBuilder sb=null;
- private ProgressDialog dialog;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- //setContentView(R.layout.login_activity);
- /*output = (TextView) findViewById(R.id.output);
- txt = (EditText) findViewById(R.id.txt);
- get = (Button) findViewById(R.id.get_button);
- post = (Button) findViewById(R.id.post_button);*/
- ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
- //http post
- try{
- HttpClient httpclient = new DefaultHttpClient();
- HttpPost httppost = new HttpPost("http://192.168.1.2:8080/test/jsp/Android/Select.jsp");
- httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
- HttpResponse response = httpclient.execute(httppost);
- HttpEntity entity = response.getEntity();
- is = entity.getContent();
- }catch(Exception e){
- Log.e("log_tag", "Error in http connection"+e.toString());
- }
- //convert response to string
- try{
- BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
- sb = new StringBuilder();
- sb.append(reader.readLine() + "\n");
- String line="0";
- while ((line = reader.readLine()) != null) {
- sb.append(line + "\n");
- }
- is.close();
- result=sb.toString();
- }catch(Exception e){
- Log.e("log_tag", "Error converting result "+e.toString());
- }
- //paring data
- int ct_id;
- String ct_name;
- try{
- jArray = new JSONArray(result);
- JSONObject json_data=null;
- for(int i=0;i<jArray.length();i++){
- json_data = jArray.getJSONObject(i);
- ct_id=json_data.getInt("id");
- ct_name=json_data.getString("user");
- }
- }
- catch(JSONException e1){
- Toast.makeText(getBaseContext(), "No City Found" ,Toast.LENGTH_LONG).show();
- } catch (ParseException e1) {
- e1.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement