我有订阅活动的代码但是当我输入发送按钮插入它我得到致命异常我在谷歌检查但我没有找到我的问题的解决方案。我在连接成功消息后收到错误。我已经添加了互联网许可,然后可能有任何问题的帮助
public class SubscribeActivity extends Activity {
String result = null;
InputStream is = null;
private EditText edittext_first;
private EditText edittext_last;
private EditText edittext_age;
private EditText edittext_email;
private EditText edittext_confirm;
private Button send;
String v5,v1,v2,v3,v4,v6;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.subscribe);
edittext_first = (EditText) findViewById(R.id.firstname);
edittext_last = (EditText) findViewById(R.id.lastname);
edittext_age = (EditText) findViewById(R.id.age1);
edittext_email = (EditText) findViewById(R.id.email);
edittext_confirm = (EditText) findViewById(R.id.confirm);
send = (Button) findViewById(R.id.send);
send.setOnClickListener(new View.OnClickListener() {
@SuppressLint("NewApi")
@Override
public void onClick(View view) {
boolean isValid = true;
if (edittext_first.getText().toString().length() <= 0) {
edittext_first.setError("VALUE REQUIRED");
isValid = false;
} else if (!edittext_first.getText().toString().matches("[a-zA-Z ]+")) {
edittext_first.setError("ENTER ONLY ALPHABETS");
isValid = false;
}
if (edittext_last.getText().toString().length() <= 0) {
edittext_last.setError("VALUE REQUIRED");
isValid = false;
} else if (!edittext_last.getText().toString().matches("[a-zA-Z ]+")) {
edittext_last.setError("ENTER ONLY ALPHABETS");
isValid = false;
}
if (edittext_age.getText().toString().length() <= 0) {
edittext_age.setError("VALUE REQUIRED");
isValid = false;
} else if (!edittext_age.getText().toString().matches("[0-9 ]+")) {
edittext_age.setError("ACCEPT NUMBERS");
isValid = false;
}
if (edittext_email.getText().toString().length() <= 0) {
edittext_email.setError("VALUE REQUIRED");
isValid = false;
} else if (!isEmailValid(edittext_email.getText().toString())) {
edittext_email.setError("INVALID EMAIL");
isValid = false;
}
if (edittext_confirm.getText().toString().length() <= 0) {
edittext_confirm.setError("VALUE REQUIRED");
isValid = false;
} else if (!isEmailValid(edittext_confirm.getText().toString())) {
edittext_email.setError("INVALID EMAIL");
isValid = false;
} else if (!edittext_confirm.getText().toString().equals(edittext_email.getText().toString())) {
edittext_confirm.setError("PASSWORD MISMATCH");
isValid = false;
}
v1 = edittext_first.getText().toString();
v2 = edittext_last.getText().toString();
v3 = edittext_age.getText().toString();
v4 = edittext_email.getText().toString();
v5 = edittext_confirm.getText().toString();
if (isValid) {
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("firstname", v1));
nameValuePairs.add(new BasicNameValuePair("lastname", v2));
nameValuePairs.add(new BasicNameValuePair("age", v3));
nameValuePairs.add(new BasicNameValuePair("email_address", v4));
nameValuePairs.add(new BasicNameValuePair("confirm_email_address", v5));
submitDataOnServer(nameValuePairs);
}
}
});
}
public void submitDataOnServer(final ArrayList<NameValuePair> nameValuePairs){
new AsyncTask<Void,Void,String>(){
@Override
protected String doInBackground(Void... params) {
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://192.168.1.137/Android/App/getInsert.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
Log.e("log_tag", "connection success ");
System.out.println("Zero");
//Toast.makeText(getApplicationContext(), "Subscription Successfull.Thank You for Subscribing you have now been added to our Mailing Lists", Toast.LENGTH_LONG).show();
} catch (Exception e) {
Log.e("log_tag", "Error in http connection " + e.toString());
//Toast.makeText(getApplicationContext(), "Connection fail", Toast.LENGTH_SHORT).show();
}
try {
System.out.println("First");
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
System.out.println("Second");
System.out.println("After Second:"+reader.readLine());
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
System.out.println("Third");
result=sb.toString();
} catch (Exception e) {
System.out.println("Fourth");
Log.e("log_tag", "Error converting result " + e.toString());
}
return null;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
try {
System.out.println("Fifth");
JSONObject json_data = new JSONObject(result);
CharSequence w = (CharSequence) json_data.get("result");
Toast.makeText(getApplicationContext(), w, Toast.LENGTH_SHORT).show();
} catch (JSONException e) {
Log.e("log_tag", "Error parsing data " + e.toString());
System.out.println("sixth");
Toast.makeText(getApplicationContext(), "JsonArray fail", Toast.LENGTH_SHORT).show();
}
}
}.execute();
}
public boolean isEmailValid(final String mailAddress) {
Pattern pattern;
Matcher matcher;
final String EMAIL_PATTERN = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@" + "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
pattern = Pattern.compile(EMAIL_PATTERN);
matcher = pattern.matcher(mailAddress);
return matcher.matches();
}
}
logcat显示组件信息错误
Logcat:
08-01 06:37:56.230: E/log_tag(3328): connection success
08-01 06:37:56.330: D/AndroidRuntime(3328): Shutting down VM
08-01 06:37:56.330: W/dalvikvm(3328): threadid=1: thread exiting with uncaught exception (group=0xb3a1bba8)
08-01 06:37:56.390: E/AndroidRuntime(3328): FATAL EXCEPTION: main
08-01 06:37:56.390: E/AndroidRuntime(3328): Process: com.example.audioplay, PID: 3328
08-01 06:37:56.390: E/AndroidRuntime(3328): java.lang.NullPointerException
08-01 06:37:56.390: E/AndroidRuntime(3328): at org.json.JSONTokener.nextCleanInternal(JSONTokener.java:116)
08-01 06:37:56.390: E/AndroidRuntime(3328): at org.json.JSONTokener.nextValue(JSONTokener.java:94)
08-01 06:37:56.390: E/AndroidRuntime(3328): at org.json.JSONObject.<init>(JSONObject.java:155)
08-01 06:37:56.390: E/AndroidRuntime(3328): at org.json.JSONObject.<init>(JSONObject.java:172)
08-01 06:37:56.390: E/AndroidRuntime(3328): at com.example.audioplay.SubscribeActivity$2.onPostExecute(SubscribeActivity.java:174)
08-01 06:37:56.390: E/AndroidRuntime(3328): at com.example.audioplay.SubscribeActivity$2.onPostExecute(SubscribeActivity.java:1)
08-01 06:37:56.390: E/AndroidRuntime(3328): at android.os.AsyncTask.finish(AsyncTask.java:632)
08-01 06:37:56.390: E/AndroidRuntime(3328): at android.os.AsyncTask.access$600(AsyncTask.java:177)
08-01 06:37:56.390: E/AndroidRuntime(3328): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
08-01 06:37:56.390: E/AndroidRuntime(3328): at android.os.Handler.dispatchMessage(Handler.java:102)
08-01 06:37:56.390: E/AndroidRuntime(3328): at android.os.Looper.loop(Looper.java:136)
08-01 06:37:56.390: E/AndroidRuntime(3328): at android.app.ActivityThread.main(ActivityThread.java:5017)
08-01 06:37:56.390: E/AndroidRuntime(3328): at java.lang.reflect.Method.invokeNative(Native Method)
08-01 06:37:56.390: E/AndroidRuntime(3328): at java.lang.reflect.Method.invoke(Method.java:515)
08-01 06:37:56.390: E/AndroidRuntime(3328): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
08-01 06:37:56.390: E/AndroidRuntime(3328): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
08-01 06:37:56.390: E/AndroidRuntime(3328): at dalvik.system.NativeStart.main(Native Method)
订阅的Edittext xml文件
subscribe.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:background="#000000"
android:gravity="center_horizontal"
android:orientation="vertical" >
<ImageView
android:id="@+id/imageView2"
android:layout_width="match_parent"
android:layout_height="170dp"
android:src="@drawable/deadoralive" />
<requestFocus
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:id="@+id/record_sofya"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Join Our Mailing List - Join Today to Receive your Free Download"
android:textColor="#FFFFFF" />
<EditText
android:id="@+id/firstname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter First Name"
android:textColor="#FFFFFF" />
<EditText
android:id="@+id/lastname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Last Name"
android:textColor="#FFFFFF" />
<EditText
android:id="@+id/age1"
android:numeric="integer"
android:maxLength="2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Age"
android:textColor="#FFFFFF" />
<EditText
android:id="@+id/email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Enter Email Address"
android:textColor="#FFFFFF" />
<EditText
android:id="@+id/confirm"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Confirm Email Address"
android:textColor="#FFFFFF" />
<Button
android:id="@+id/send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#000000"
android:text="Send"
android:textColor="#FFFFFF" />
</LinearLayout>
</LinearLayout>
答案 0 :(得分:1)
在 doInBackground 中,请返回null
来自return result
JSON
或任何其他字符串(您想在doInBackground
中使用)
public void submitDataOnServer(final ArrayList<NameValuePair> nameValuePairs){
new AsyncTask<Void,Void,String>(){
@Override
protected String doInBackground(Void... params) {
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://192.168.1.137/Android/App/getInsert.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
Log.e("log_tag", "connection success ");
System.out.println("Zero");
//Toast.makeText(getApplicationContext(), "Subscription Successfull.Thank You for Subscribing you have now been added to our Mailing Lists", Toast.LENGTH_LONG).show();
} catch (Exception e) {
Log.e("log_tag", "Error in http connection " + e.toString());
//Toast.makeText(getApplicationContext(), "Connection fail", Toast.LENGTH_SHORT).show();
}
try {
System.out.println("First");
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
System.out.println("Second");
System.out.println("After Second:"+reader.readLine());
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
System.out.println("Third");
result=sb.toString();
} catch (Exception e) {
System.out.println("Fourth");
Log.e("log_tag", "Error converting result " + e.toString());
}
return result;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
try {
System.out.println("Fifth");
JSONObject json_data = new JSONObject(result);
CharSequence w = (CharSequence) json_data.get("result");
Toast.makeText(getApplicationContext(), w, Toast.LENGTH_SHORT).show();
} catch (JSONException e) {
Log.e("log_tag", "Error parsing data " + e.toString());
System.out.println("sixth");
Toast.makeText(getApplicationContext(), "JsonArray fail", Toast.LENGTH_SHORT).show();
}
}
}.execute();
}