JSONparsing or AsyncTask or settext not working

穿精又带淫゛_ 提交于 2020-01-06 14:55:31

问题


in this question I am trying to take some input and then posting them to a url and then json parsing the response and then showing the output through textView but my final page is coming blank .. I have attached all my java and xml file and log file

{
"user_id": "23",
"firstname": "",
"lastname": "",
"email": "9654008791",
"isd_code": "91‌​",
"mobile": "9654008791",
"gender": "",
"dob": "0000-00-00",
"image": null,
"status": "0",
‌​"verification_key": "7062"
}

Main Activity.java

package com.example.omg;

import java.io.IOException;
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.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.SimpleAdapter;

public class MainActivity extends Activity implements OnClickListener{

    EditText isd_code,mob_num,device_token,dev;
    Button submit;

        String sisd_code ;
        String smob_num ;
        String sdevice_token ;
        String sdev ;

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

        isd_code=(EditText)findViewById(R.id.etisd_code);
        mob_num=(EditText)findViewById(R.id.etmob_num);
        device_token=(EditText)findViewById(R.id.etdev_code);
        dev=(EditText)findViewById(R.id.etdev);
        submit=(Button)findViewById(R.id.button1);
        submit.setOnClickListener(this);

        }


    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub

    sisd_code = isd_code.getText().toString();
    smob_num = mob_num.getText().toString();
    sdevice_token = device_token.getText().toString();
    sdev = dev.getText().toString();

    String sarray[]={sisd_code,smob_num,sdevice_token,sdev};

    Bundle basket =new Bundle();
     basket.putStringArray("key",sarray);
     Intent a= new Intent(MainActivity.this,output.class);
     a.putExtras(basket);
     startActivity(a);
    }
    }

JSONparser.java

package com.example.omg;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.util.Log;
import android.widget.Toast;

public class JSONparser {
    static InputStream is = null;
    static JSONObject jObj = null;
    String json;





public  JSONObject getJSONFromUrl(String url,String smob_num,String sisd_code ,String sdevice_token ,String sdev )
    {
try{
 HttpClient httpclient = new DefaultHttpClient();
 HttpPost httppost = new HttpPost(url);
 List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(4);
        nameValuePairs.add(new BasicNameValuePair("isd_code", "sisd_code"));
        nameValuePairs.add(new BasicNameValuePair("mob_num", "smob_num"));
        nameValuePairs.add(new BasicNameValuePair("device_token", "sdevice_token"));
        nameValuePairs.add(new BasicNameValuePair("device", "sdev"));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        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 jObj;

}
}

OUTPUT.JAVA

package com.example.omg;

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

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.TextView;

public class output extends Activity{

    String url ="http://txtomg.universe.in/mobile/?node=account/signup";
    private static final String TAG_STATUS ="status";
    private static final String TAG_USER_ID = "user_id";
    private static final String TAG_FIRST_NAME = "firstname";
    private static final String TAG_LAST_NAME = "lastname";
    private static final String TAG_EMAIL = "email";
    private static final String TAG_DOB = "dob";
    private static final String TAG_ISD_CODE = "isd_code";
    private static final String TAG_IMAGE = "image";
    private static final String TAG_MOBILE = "mobile";
    private static final String TAG_VERIFICATION_KEY = "verification_key";
    private static final String TAG_GENDER = "gender";
    JSONArray contacts = null;
    String gotbread[];
    TextView efname,elname,edob,estatus,eisd_code,ever_key,egender,eemail,ephone,euser_id,eimage;
    JSONObject d,c ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list);
        Bundle gotbasket=getIntent().getExtras();
                gotbread=gotbasket.getStringArray("key");
            String  code= gotbread[0];
            String  mobile=gotbread[1];
            String  token=gotbread[2];
            String  status=gotbread[3];

                 ultimate ult = new ultimate();
                ult.execute(code,mobile,token,status);

    }
    public void display(JSONObject e){

        c = e;
                efname=(TextView)findViewById(R.id.edfname);
                 elname=(TextView)findViewById(R.id.edlname);
                 edob=(TextView)findViewById(R.id.eddob);
                 estatus=(TextView)findViewById(R.id.edstatus);
                 eisd_code=(TextView)findViewById(R.id.edisdcode);
                 ever_key=(TextView)findViewById(R.id.edverkey);
                 egender=(TextView)findViewById(R.id.edgender);
                 eemail=(TextView)findViewById(R.id.edemail);
                 ephone=(TextView)findViewById(R.id.edmobile);
                euser_id =(TextView)findViewById(R.id.eduserid);
                eimage=(TextView)findViewById(R.id.edimage);

                  // Creating JSON Parser instance


                  // getting JSON string from URL

                try {


                     int user_id = c.getInt(TAG_USER_ID);
                      String fname = c.getString(TAG_FIRST_NAME);
                      String lname = c.getString(TAG_LAST_NAME);

                     String email = c.getString(TAG_EMAIL);

                      int mobile= c.getInt(TAG_MOBILE);
                      int isd_code= c.getInt(TAG_ISD_CODE);
                     String gender = c.getString(TAG_GENDER);

                      int dob= c.getInt(TAG_DOB);
                      String image= c.getString(TAG_IMAGE);

                      int status = c.getInt(TAG_STATUS);
                      int ver_key= c.getInt(TAG_VERIFICATION_KEY); 
                      efname.setText(fname);
                         elname.setText(lname);
                         edob.setText(String.valueOf(dob));
                         estatus.setText(String.valueOf(status));
                         eisd_code.setText(String.valueOf(isd_code));
                         ever_key.setText(String.valueOf(ver_key));
                         egender.setText(gender);
                         eemail.setText(email);
                         ephone.setText(String.valueOf(mobile));
                        euser_id.setText(String.valueOf(user_id));
                        eimage.setText(image);
                } catch (Throwable e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }

//              



    }   

    private class ultimate extends AsyncTask<String,String,JSONObject>
    {

        @Override
        protected JSONObject doInBackground(String... params) {
            // TODO Auto-generated method stub
             String ucode = params[0];
             String umobile = params[1];
             String utoken = params[2];
             String ustatus = params[3];

             JSONparser jParser = new JSONparser();
            d = jParser.getJSONFromUrl(url, umobile, ucode, utoken, ustatus);


            return d;
        }

        @Override
        protected void onPostExecute(JSONObject result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);
            display(result);    
        }
        }

   }

MAIN.XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="26dp"
        android:text="ISD CODE:" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="25dp"
        android:text="MOBILE:" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView2"
        android:layout_below="@+id/textView2"
        android:layout_marginTop="28dp"
        android:text="DEVICE_TOKEN" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView3"
        android:layout_below="@+id/textView3"
        android:layout_marginTop="33dp"
        android:text="DEVICE" />

    <EditText
        android:id="@+id/etisd_code"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignTop="@+id/textView1"
        android:ems="10"
        android:inputType="textPassword" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/etdev"
        android:layout_marginTop="38dp"
        android:layout_toRightOf="@+id/textView3"
        android:text="Submit" />

    <EditText
        android:id="@+id/etmob_num"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/etisd_code"
        android:layout_alignTop="@+id/textView2"
        android:ems="10" />

    <EditText
        android:id="@+id/etdev_code"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView3"
        android:layout_alignBottom="@+id/textView3"
        android:layout_alignParentRight="true"
        android:ems="10" />

    <EditText
        android:id="@+id/etdev"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView4"
        android:layout_alignBottom="@+id/textView4"
        android:layout_alignParentRight="true"
        android:ems="10" />

</RelativeLayout>

LIST.XML

   <?xml version="1.0" encoding="utf-8"?>
  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/edfname"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10" 
        android:text="TextView">
    </TextView>

    <TextView
        android:id="@+id/edlname"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10" 
        android:text="TextView"/>

    <TextView
        android:id="@+id/eddob"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10" 
        android:text="TextView"/>

    <TextView
        android:id="@+id/edmobile"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10" 
        android:text="TextView"/>

    <TextView
        android:id="@+id/eduserid"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10" 
        android:text="TextView"/>

    <TextView
        android:id="@+id/edverkey"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10" 
        android:text="TextView"/>

    <TextView
        android:id="@+id/edisdcode"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10" 
        android:text="TextView"/>

    <TextView
        android:id="@+id/edemail"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10" 
        android:text="TextView"/>

    <TextView
        android:id="@+id/edimage"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:text="TextView" />


    <TextView
        android:id="@+id/edgender"
        android:layout_width="match_parent"
         android:layout_height="wrap_content"
        android:text="TextView"
        android:ems="10" />

    <TextView
        android:id="@+id/edstatus"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10" 
        android:text="TextView">

    </TextView>


</LinearLayout>

The Crash Log

06-27 01:10:43.080: I/global(590): Loaded time zone names for en_US in 5032ms.
06-27 01:10:43.110: W/System.err(590): org.json.JSONException: No value for user_id
06-27 01:10:43.110: W/System.err(590):  at org.json.JSONObject.get(JSONObject.java:354)
06-27 01:10:43.110: W/System.err(590):  at org.json.JSONObject.getString(JSONObject.java:510)
06-27 01:10:43.110: W/System.err(590):  at com.example.omg.output.display(output.java:68)
06-27 01:10:43.110: W/System.err(590):  at com.example.omg.output$ultimate.onPostExecute(output.java:127)
06-27 01:10:43.110: W/System.err(590):  at com.example.omg.output$ultimate.onPostExecute(output.java:1)
06-27 01:10:43.121: W/System.err(590):  at android.os.AsyncTask.finish(AsyncTask.java:417)
06-27 01:10:43.121: W/System.err(590):  at android.os.AsyncTask.access$300(AsyncTask.java:127)
06-27 01:10:43.121: W/System.err(590):  at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
06-27 01:10:43.121: W/System.err(590):  at android.os.Handler.dispatchMessage(Handler.java:99)
06-27 01:10:43.121: W/System.err(590):  at android.os.Looper.loop(Looper.java:123)
06-27 01:10:43.130: W/System.err(590):  at android.app.ActivityThread.main(ActivityThread.java:3683)
06-27 01:10:43.130: W/System.err(590):  at java.lang.reflect.Method.invokeNative(Native Method)
06-27 01:10:43.130: W/System.err(590):  at java.lang.reflect.Method.invoke(Method.java:507)
06-27 01:10:43.130: W/System.err(590):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
06-27 01:10:43.130: W/System.err(590):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-27 01:10:43.130: W/System.err(590):  at dalvik.system.NativeStart.main(Native Method)

回答1:


in your ultimate class, inside doInBackground you are returning the class member c which is uninitialized instead of d that is the one that takes the result of jParser.getJSONFromUrl. you have to return d



来源:https://stackoverflow.com/questions/17328516/jsonparsing-or-asynctask-or-settext-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!