gpt4 book ai didi

android - 向 Bluemix 部署的 Node.js 应用程序发送 httppost 请求时出现 "Unfortunately app has stopped"错误

转载 作者:行者123 更新时间:2023-11-29 17:38:05 26 4
gpt4 key购买 nike

我正在创建一个 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)

最佳答案

该错误是因为您正在尝试在 Android 的主线程和 strictmode 上进行网络调用正在激活策略以阻止它。您可以在 android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork 这一行的堆栈跟踪中看到这一点。

您应该考虑在单独的线程上执行网络调用。严格模式旨在帮助为最终用户提供更好的体验。

关于android - 向 Bluemix 部署的 Node.js 应用程序发送 httppost 请求时出现 "Unfortunately app has stopped"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29846057/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com