JSONparsing或AsyncTask的或没有的setText工作 [英] JSONparsing or AsyncTask or settext not working

查看:189
本文介绍了JSONparsing或AsyncTask的或没有的setText工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这个问题上,我试图采取一些输入,然后将它们张贴到一个URL,然后JSON解析响应,然后显示输出通过TextView的,但我的最后一页即将空白..
我重视我所有的Java和XML文件和日志文件

  {
USER_ID:23,
名字: ,
姓: ,
电子邮件:9654008791,
isd_ code:91,
手机:9654008791,
性别: ,
DOB:0000-00-00,
形象:空,
状态:0
verification_key:7062
}

主要Activity.java

 包com.example.omg;进口java.io.IOException异常;
进口的java.util.ArrayList;
进口的java.util.HashMap;进口org.json.JSONArray;
进口org.json.JSONException;
进口org.json.JSONObject;进口android.app.Activity;
进口android.content.Intent;
进口android.os.Bundle;
进口android.view.Menu;
进口android.view.View;
进口android.view.View.OnClickListener;
进口android.widget.Button;
进口android.widget.EditText;
进口android.widget.ListAdapter;
进口android.widget.SimpleAdapter;公共类MainActivity扩展活动实现OnClickListener {    的EditText isd_ code,mob_num,device_token,开发;
    按钮提交;        字符串sisd_ code;
        串smob_num;
        串sdevice_token;
        字符串发展局局长;    @覆盖
    保护无效的onCreate(捆绑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);
        开发=(EditText上)findViewById(R.id.etdev);
        提交=(按钮)findViewById(R.id.button1);
        submit.setOnClickListener(本);        }
    @覆盖
    公共无效的onClick(查看为arg0){
        // TODO自动生成方法存根    sisd_ code = isd_ code.getText()的toString()。
    。smob_num = mob_num.getText()的toString();
    sdevice_token = device_token.getText()的toString()。
    。发展局局长= dev.getText()的toString();    字符串sarray [] = {sisd_ code,smob_num,sdevice_token,发展局局长};    包篮=新包();
     basket.putStringArray(钥匙,sarray);
     意图=新意图(MainActivity.this,output.class);
     a.putExtras(篮);
     startActivity(一);
    }
    }

JSONparser.java

 包com.example.omg;进口java.io.BufferedReader中;
进口java.io.IOException异常;
进口的java.io.InputStream;
进口java.io.InputStreamReader中;
进口java.io.UnsupportedEncodingException;
进口的java.util.ArrayList;
进口的java.util.List;进口org.apache.http.HttpEntity;
进口org.apache.http.Htt presponse;
进口org.apache.http.NameValuePair;
进口org.apache.http.client.ClientProtocolException;
进口org.apache.http.client.HttpClient;
进口org.apache.http.client.entity.UrlEn codedFormEntity;
进口org.apache.http.client.methods.HttpPost;
进口org.apache.http.impl.client.DefaultHttpClient;
进口org.apache.http.message.BasicNameValuePair;
进口org.json.JSONException;
进口org.json.JSONObject;进口android.app.Activity;
进口android.util.Log;
进口android.widget.Toast;公共类JSONparser {
    静态InputStream为= NULL;
    静态的JSONObject jObj = NULL;
    JSON字符串;公众的JSONObject getJSONFromUrl(URL字符串,字符串smob_num,字符串sisd_ code,字符串sdevice_token,字符串发展局局长)
    {
尝试{
 HttpClient的HttpClient的=新DefaultHttpClient();
 HttpPost httppost =新HttpPost(URL);
 清单<&的NameValuePair GT; namevaluepairs中=新的ArrayList<&的NameValuePair GT;(4);
        nameValuePairs.add(新BasicNameValuePair(isd_ code,sisd_ code));
        nameValuePairs.add(新BasicNameValuePair(mob_num,smob_num));
        nameValuePairs.add(新BasicNameValuePair(device_token,sdevice_token));
        nameValuePairs.add(新BasicNameValuePair(设备,发展局局长));
        httppost.setEntity(新UrlEn codedFormEntity(namevaluepairs中));
        HTT presponse HTT presponse = httpclient.execute(httppost);
        HttpEntity httpEntity = HTT presponse.getEntity();
        是= httpEntity.getContent();
}赶上(UnsupportedEncodingException五){
    e.printStackTrace();
}赶上(ClientProtocolException E){
    e.printStackTrace();
}赶上(IOException异常五){
    e.printStackTrace();
}
        尝试{
            读者的BufferedReader =新的BufferedReader(新的InputStreamReader(
                    是,ISO-8859-1),8);
            StringBuilder的SB =新的StringBuilder();
            串线= NULL;
            而((行= reader.readLine())!= NULL){
                sb.append(行+\\ n);
            }
            is.close();
            JSON = sb.toString();
        }赶上(例外五){
            Log.e(缓冲区错误,错误转换结果+ e.toString());
        }        //尝试分析字符串到一个JSON对象
        尝试{
            jObj =新的JSONObject(JSON);
        }赶上(JSONException E){
            Log.e(JSON解析器,错误分析数据+ e.toString());
        }返回jObj;}
}

OUTPUT.JAVA

 包com.example.omg;进口org.json.JSONArray;
进口org.json.JSONObject;进口android.app.Activity;
进口android.os.AsyncTask;
进口android.os.Bundle;
进口android.widget.TextView;公共类输出扩展活动{    字符串URL =htt​​p://txtomg.universe.in/mobile/?node=account/signup;
    私有静态最后弦乐TAG_STATUS =身份;
    私有静态最后弦乐TAG_USER_ID =USER_ID;
    私有静态最后弦乐TAG_FIRST_NAME =名字;
    私有静态最后弦乐TAG_LAST_NAME =姓氏;
    私有静态最后弦乐TAG_EMAIL =电子邮件;
    私有静态最后弦乐TAG_DOB =DOB;
    私有静态最后弦乐TAG_ISD_ code =isd_ code;
    私有静态最后弦乐TAG_IMAGE =形象;
    私有静态最后弦乐TAG_MOBILE =移动;
    私有静态最后弦乐TAG_VERIFICATION_KEY =verification_key;
    私有静态最后弦乐TAG_GENDER =性别;
    JSONArray接触= NULL;
    串gotbread [];
    TextView的efname,elname,edob,estatus,eisd_ code,ever_key,egender,eemail,丰新闻,euser_id,eimage;
    JSONObject的D,C;
    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        // TODO自动生成方法存根
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.list);
        捆绑gotbasket = getIntent()getExtras()。
                gotbread = gotbasket.getStringArray(钥匙);
            字符串code = gotbread [0];
            字符串移动= gotbread [1];
            字符串标记= gotbread [2];
            串状态= gotbread [3];                 最终ULT =新的极致();
                ult.execute(code,手机,令牌状态);    }
    公共无效显示(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.edisd code);
                 ever_key =(的TextView)findViewById(R.id.edverkey);
                 egender =(的TextView)findViewById(R.id.edgender);
                 eemail =(的TextView)findViewById(R.id.edemail);
                 丰新闻=(的TextView)findViewById(R.id.edmobile);
                euser_id =(的TextView)findViewById(R.id.eduserid);
                eimage =(的TextView)findViewById(R.id.edimage);                  //创建JSON解析器实例
                  // URL从获取JSON字符串                尝试{
                     INT USER_ID = c.getInt(TAG_USER_ID);
                      字符串FNAME = c.getString(TAG_FIRST_NAME);
                      字符串LNAME = c.getString(TAG_LAST_NAME);                     字符串email = c.getString(TAG_EMAIL);                      INT移动= c.getInt(TAG_MOBILE);
                      INT isd_ code = c.getInt(TAG_ISD_ code);
                     字符串性别= c.getString(TAG_GENDER);                      INT DOB = c.getInt(TAG_DOB);
                      字符串图像= c.getString(TAG_IMAGE);                      INT状态= 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(状态));
                         eisd_ code.setText(将String.valueOf(isd_ code));
                         ever_key.setText(将String.valueOf(ver_key));
                         egender.setText(性别);
                         eemail.setText(电子邮件);
                         ephone.setText(将String.valueOf(移动));
                        euser_id.setText(将String.valueOf(USER_ID));
                        eimage.setText(图片);
                }赶上(E1的Throwable){
                    // TODO自动生成catch块
                    e1.printStackTrace();
                }//    }    私有类最终扩展的AsyncTask<字符串,字符串,JSONObject的>
    {        @覆盖
        受保护的JSONObject doInBackground(字符串... PARAMS){
            // TODO自动生成方法存根
             字符串U code = PARAMS [0];
             串umobile =参数[1];
             字符串utoken =参数[2];
             串USTATUS =参数[3];             JSONparser jParser =新JSONparser();
            D = jParser.getJSONFromUrl(URL,umobile,U code,utoken,USTATUS);
            返回D组;
        }        @覆盖
        保护无效onPostExecute(JSONObject的结果){
            // TODO自动生成方法存根
            super.onPostExecute(结果);
            显示(结果);
        }
        }   }

main.xml中

 <的RelativeLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:paddingBottom会=@扪/ activity_vertical_margin
    机器人:paddingLeft =@扪/ activity_horizo​​ntal_margin
    机器人:paddingRight =@扪/ activity_horizo​​ntal_margin
    机器人:paddingTop =@扪/ activity_vertical_margin
    工具:上下文=MainActivity。>    <的TextView
        机器人:ID =@ + ID / textView1
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignParentLeft =真
        机器人:layout_alignParentTop =真
        机器人:layout_marginTop =26dp
        机器人:文字=ISD code:/>    <的TextView
        机器人:ID =@ + ID / textView2
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignLeft =@ + ID / textView1
        机器人:layout_below =@ + ID / textView1
        机器人:layout_marginTop =25dp
        机器人:文字=MOBILE/>    <的TextView
        机器人:ID =@ + ID / textView3
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignLeft =@ + ID / textView2
        机器人:layout_below =@ + ID / textView2
        机器人:layout_marginTop =28dp
        机器人:文字=DEVICE_TOKEN/>    <的TextView
        机器人:ID =@ + ID / textView4
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignLeft =@ + ID / textView3
        机器人:layout_below =@ + ID / textView3
        机器人:layout_marginTop =33dp
        机器人:文本=设备/>    <的EditText
        机器人:ID =@ + ID / etisd_ code
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignParentRight =真
        机器人:layout_alignTop =@ + ID / textView1
        机器人:EMS =10
        安卓的inputType =textPassword>        < requestFocus的/>
    < /&的EditText GT;    <按钮
        机器人:ID =@ + ID /按钮1
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_below =@ + ID / etdev
        机器人:layout_marginTop =38dp
        机器人:layout_toRightOf =@ + ID / textView3
        机器人:文字=提交/>    <的EditText
        机器人:ID =@ + ID / etmob_num
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignLeft =@ + ID / etisd_ code
        机器人:layout_alignTop =@ + ID / textView2
        机器人:EMS =10/>    <的EditText
        机器人:ID =@ + ID / etdev_ code
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignBaseline =@ + ID / textView3
        机器人:layout_alignBottom =@ + ID / textView3
        机器人:layout_alignParentRight =真
        机器人:EMS =10/>    <的EditText
        机器人:ID =@ + ID / etdev
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignBaseline =@ + ID / textView4
        机器人:layout_alignBottom =@ + ID / textView4
        机器人:layout_alignParentRight =真
        机器人:EMS =10/>< / RelativeLayout的>

LIST.XML

 <?XML版本=1.0编码=UTF-8&GT?;
  < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:方向=垂直>    <的TextView
        机器人:ID =@ + ID / edfname
        机器人:layout_width =match_parent
        机器人:layout_height =WRAP_CONTENT
        机器人:EMS =10
        机器人:文字=TextView的>
    < / TextView的>    <的TextView
        机器人:ID =@ + ID / edlname
        机器人:layout_width =match_parent
        机器人:layout_height =WRAP_CONTENT
        机器人:EMS =10
        机器人:文字=TextView的/>    <的TextView
        机器人:ID =@ + ID / eddob
        机器人:layout_width =match_parent
        机器人:layout_height =WRAP_CONTENT
        机器人:EMS =10
        机器人:文字=TextView的/>    <的TextView
        机器人:ID =@ + ID / edmobile
        机器人:layout_width =match_parent
        机器人:layout_height =WRAP_CONTENT
        机器人:EMS =10
        机器人:文字=TextView的/>    <的TextView
        机器人:ID =@ + ID / eduserid
        机器人:layout_width =match_parent
        机器人:layout_height =WRAP_CONTENT
        机器人:EMS =10
        机器人:文字=TextView的/>    <的TextView
        机器人:ID =@ + ID / edverkey
        机器人:layout_width =match_parent
        机器人:layout_height =WRAP_CONTENT
        机器人:EMS =10
        机器人:文字=TextView的/>    <的TextView
        机器人:ID =@ + ID / edisd code
        机器人:layout_width =match_parent
        机器人:layout_height =WRAP_CONTENT
        机器人:EMS =10
        机器人:文字=TextView的/>    <的TextView
        机器人:ID =@ + ID / edemail
        机器人:layout_width =match_parent
        机器人:layout_height =WRAP_CONTENT
        机器人:EMS =10
        机器人:文字=TextView的/>    <的TextView
        机器人:ID =@ + ID / edimage
        机器人:layout_width =match_parent
        机器人:layout_height =WRAP_CONTENT
        机器人:EMS =10
        机器人:文字=TextView的/>
    <的TextView
        机器人:ID =@ + ID / edgender
        机器人:layout_width =match_parent
         机器人:layout_height =WRAP_CONTENT
        机器人:文字=TextView的
        机器人:EMS =10/>    <的TextView
        机器人:ID =@ + ID / edstatus
        机器人:layout_width =match_parent
        机器人:layout_height =WRAP_CONTENT
        机器人:EMS =10
        机器人:文字=TextView的>    < / TextView的>
< / LinearLayout中>

崩溃日志

  1月6日至27日:10:43.080:I /全局(590):在5032ms的EN_US加载时区名称。
一月六日至27日:10:43.110:W / System.err的(590):org.json.JSONException:为USER_ID没有价值
一月六日至27日:10:43.110:W / System.err的(590):在org.json.JSONObject.get(JSONObject.java:354)
一月六日至27日:10:43.110:W / System.err的(590):在org.json.JSONObject.getString(JSONObject.java:510)
一月六日至27日:10:43.110:W / System.err的(590):在com.example.omg.output.display(output.java:68)
一月六日至27日:10:43.110:W / System.err的(590):在com.example.omg.output $ ultimate.onPostExecute(output.java:127)
一月六日至27日:10:43.110:W / System.err的(590):在com.example.omg.output $ ultimate.onPostExecute(output.java:1)
一月六日至27日:10:43.121:W / System.err的(590):在android.os.AsyncTask.finish(AsyncTask.java:417)
一月六日至27日:10:43.121:W / System.err的(590):在android.os.AsyncTask.access $ 300(AsyncTask.java:127)
一月六日至27日:10:43.121:W / System.err的(590):在android.os.AsyncTask $ InternalHandler.handleMessage(AsyncTask.java:429)
一月六日至27日:10:43.121:W / System.err的(590):在android.os.Handler.dispatchMessage(Handler.java:99)
一月六日至27日:10:43.121:W / System.err的(590):在android.os.Looper.loop(Looper.java:123)
一月六日至27日:10:43.130:W / System.err的(590):在android.app.ActivityThread.main(ActivityThread.java:3683)
一月六日至27日:10:43.130:W / System.err的(590):在java.lang.reflect.Method.invokeNative(本机方法)
一月六日至27日:10:43.130:W / System.err的(590):在java.lang.reflect.Method.invoke(Method.java:507)
一月六日至27日:10:43.130:W / System.err的(590):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:839)
一月六日至27日:10:43.130:W / System.err的(590):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
一月六日至27日:10:43.130:W / System.err的(590):在dalvik.system.NativeStart.main(本机方法)


解决方案

终极类,在 doInBackground 您正在返回的类成员 C 这是未初始化的,而不是 D 那就是采用<$的结果之一C $ C> jParser.getJSONFromUrl 。你必须返回 D

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)

解决方案

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

这篇关于JSONparsing或AsyncTask的或没有的setText工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆