我正在创建一个Android应用,但在使用httppost
方法将参数发送到Bluemix部署的Node.js服务器时,我收到“遗憾的应用已停止”错误。
任何人都可以帮我解决这个问题吗?
我有查询,无法从Node.js收到门票链接的响应:Am not able to send my request parameters to bluemix deployed node.js
如下所示的Jave代码:
package com.example.rest;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
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 android.support.v7.app.ActionBarActivity;
import android.app.Dialog;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Adapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
String url = "http://mobilefeedbackform.mybluemix.net/";
List<NameValuePair> params;
SharedPreferences pref;
Dialog reset;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button breg1 = (Button) findViewById(R.id.buttonreg);
breg1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
final EditText Name =(EditText) findViewById(R.id.editTextreg5);
final EditText Email =(EditText) findViewById(R.id.editTextreg);
final EditText Passwd =(EditText) findViewById(R.id.editTextreg2);
if(Name.getText().toString().length()==0)
{
Toast.makeText(MainActivity.this,
"Please enter Name", Toast.LENGTH_LONG)
.show();
return;
}
else if(Email.getText().toString().length()==0)
{
Toast.makeText(MainActivity.this,
"Please enter EmailId", Toast.LENGTH_LONG)
.show();
return;
}
else if(Passwd.getText().toString().length()==0)
{
Toast.makeText(MainActivity.this,
"Please enter Password", Toast.LENGTH_LONG)
.show();
return;
}
else
{
String nametxt = Name.getText().toString();
String emailtxt = Email.getText().toString();
String passwdtxt = Passwd.getText().toString();
// Creating HTTP client
HttpClient httpClient = new DefaultHttpClient();
// Creating HTTP Post
HttpPost httpPost = new HttpPost("http://mobilefeedbackform.mybluemix.net");
// Building post parameters
// key and value pair
List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(3);
nameValuePair.add(new BasicNameValuePair("name", nametxt));
nameValuePair.add(new BasicNameValuePair("email", nametxt));
nameValuePair.add(new BasicNameValuePair("password", passwdtxt));
// Url Encoding the POST parameters
try {
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair));
} catch (UnsupportedEncodingException e) {
// writing error to Log
e.printStackTrace();
}
// Making HTTP Request
try {
HttpResponse response = httpClient.execute(httpPost);
// writing response to log
Log.d("Http Response:", response.toString());
} catch (ClientProtocolException e) {
// writing exception to log
e.printStackTrace();
} catch (IOException e) {
// writing exception to log
e.printStackTrace();
}
}
}
});
}
}
我的错误日志如下:
04-24 18:26:13.799: E/AndroidRuntime(528): FATAL EXCEPTION: main
04-24 18:26:13.799: E/AndroidRuntime(528):android.os.NetworkOnMainThreadException
04-24 18:26:13.799: E/AndroidRuntime(528): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
04-24 18:26:13.799: E/AndroidRuntime(528): at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
04-24 18:26:13.799: E/AndroidRuntime(528): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
04-24 18:26:13.799: E/AndroidRuntime(528): at java.net.InetAddress.getAllByName(InetAddress.java:220)
04-24 18:26:13.799: E/AndroidRuntime(528): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
04-24 18:26:13.799: E/AndroidRuntime(528): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
04-24 18:26:13.799: E/AndroidRuntime(528): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
04-24 18:26:13.799: E/AndroidRuntime(528): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
04-24 18:26:13.799: E/AndroidRuntime(528): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
04-24 18:26:13.799: E/AndroidRuntime(528): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
04-24 18:26:13.799: E/AndroidRuntime(528): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
04-24 18:26:13.799: E/AndroidRuntime(528): at com.example.rest.MainActivity$1.onClick(MainActivity.java:106)
04-24 18:26:13.799: E/AndroidRuntime(528): at android.view.View.performClick(View.java:3511)
04-24 18:26:13.799: E/AndroidRuntime(528): at android.view.View$PerformClick.run(View.java:14105)
04-24 18:26:13.799: E/AndroidRuntime(528): at android.os.Handler.handleCallback(Handler.java:605)
04-24 18:26:13.799: E/AndroidRuntime(528): at android.os.Handler.dispatchMessage(Handler.java:92)
04-24 18:26:13.799: E/AndroidRuntime(528): at android.os.Looper.loop(Looper.java:137)
04-24 18:26:13.799: E/AndroidRuntime(528): at android.app.ActivityThread.main(ActivityThread.java:4424)
04-24 18:26:13.799: E/AndroidRuntime(528): at java.lang.reflect.Method.invokeNative(Native Method)
04-24 18:26:13.799: E/AndroidRuntime(528): at java.lang.reflect.Method.invoke(Method.java:511)
04-24 18:26:13.799: E/AndroidRuntime(528): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
04-24 18:26:13.799: E/AndroidRuntime(528): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
04-24 18:26:13.799: E/AndroidRuntime(528): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:3)
该错误是因为您正在Android中的主线程上尝试网络调用,并且正在激活strictmode策略以阻止它。您可以在此行android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork
的堆栈跟踪中看到此信息。
您应该查看在单独的线程上执行网络调用。 Strictmode可以帮助最终用户获得更好的体验。