在这个问题中我试图获取一些输入,然后将它们发布到一个url,然后json解析响应,然后通过textView显示输出,但我的最后一页是空白的.. 我附加了所有的java和xml文件以及日志文件
主要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 java.io.IOException;
import org.json.JSONArray;
import org.json.JSONObject;
import android.app.Activity;
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[];
String code,status,mobile,token;
TextView efname,elname,edob,estatus,eisd_code,ever_key,egender,eemail,ephone,euser_id,eimage;
@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");
code= gotbread[0];
mobile=gotbread[1];
token=gotbread[2];
status=gotbread[3];
JSONObject c ;
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
JSONparser jParser = new JSONparser();
// getting JSON string from URL
try {
c = jParser.getJSONFromUrl(url, mobile, code, token, status);
int user_id = c.getInt(TAG_USER_ID);
String fname = c.getString(TAG_FIRST_NAME);
String email = c.getString(TAG_EMAIL);
String gender = c.getString(TAG_GENDER);
String lname = c.getString(TAG_LAST_NAME);
int dob= c.getInt(TAG_DOB);
String image= c.getString(TAG_IMAGE);
int ver_key= c.getInt(TAG_VERIFICATION_KEY);
int mobile= c.getInt(TAG_MOBILE);
int isd_code= c.getInt(TAG_ISD_CODE);
int status = c.getInt(TAG_STATUS);
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 (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (Throwable e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
//
}
}
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>
崩溃日志
06-26 18:07:58.965: W/KeyCharacterMap(396): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
06-26 18:08:12.876: D/dalvikvm(396): GC_FOR_MALLOC freed 8112 objects / 324752 bytes in 106ms
06-26 18:08:13.236: W/System.err(396): org.json.JSONException: No value for user_id
06-26 18:08:13.236: W/System.err(396): at org.json.JSONObject.get(JSONObject.java:354)
06-26 18:08:13.236: W/System.err(396): at org.json.JSONObject.getInt(JSONObject.java:443)
06-26 18:08:13.236: W/System.err(396): at com.example.omg.output.onCreate(output.java:63)
06-26 18:08:13.236: W/System.err(396): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-26 18:08:13.236: W/System.err(396): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
06-26 18:08:13.246: W/System.err(396): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
06-26 18:08:13.246: W/System.err(396): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
06-26 18:08:13.246: W/System.err(396): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
06-26 18:08:13.256: W/System.err(396): at android.os.Handler.dispatchMessage(Handler.java:99)
06-26 18:08:13.256: W/System.err(396): at android.os.Looper.loop(Looper.java:123)
06-26 18:08:13.256: W/System.err(396): at android.app.ActivityThread.main(ActivityThread.java:4627)
06-26 18:08:13.256: W/System.err(396): at java.lang.reflect.Method.invokeNative(Native Method)
06-26 18:08:13.256: W/System.err(396): at java.lang.reflect.Method.invoke(Method.java:521)
06-26 18:08:13.256: W/System.err(396): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
06-26 18:08:13.256: W/System.err(396): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
06-26 18:08:13.256: W/System.err(396): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:2)
edob.setText(dob);
你不能直接将int放在textview中。你需要将它转换为一个字符串。
edob.setText(String.valueOf(dob));
如果你用int
参数调用setText,android会在资源中寻找一个String
的值
答案 1 :(得分:1)
设置这样的值。
efname.setText(""+fname);
通过使用它你也可以设置int值。 所以试试吧。
答案 2 :(得分:1)
你试过这个吗?
edob.setText(Integer.toString(dob));