[英]Android App Crashes When Sending Data To Server
I am trying to send some data to my server which is publicly available on the internet. 我正在尝试将一些数据发送到我的服务器,该数据在Internet上公开可用。 The app gets started as a service after boot is completed (permission is added), how ever the app crashes.
引导完成(添加权限)后,该应用即作为服务启动,无论该应用发生崩溃。 I have narrowed the code down.
我缩小了代码范围。 It seems like it crashes when it is trying to send data to the server.
尝试将数据发送到服务器时似乎崩溃了。
Below is my code. 下面是我的代码。 I really appreciate if you guys can help me on this.
如果你们能在这方面帮助我,我非常感谢。
package com.example.bootstart;
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.os.AsyncTask;
import android.util.Log;
class SendData extends AsyncTask<Void, Void, Boolean> {
public void sendinfo(){
// Creating HTTP client
HttpClient httpClient = new DefaultHttpClient();
// Creating HTTP Post
HttpPost httpPost = new HttpPost(
"http://www.mydomain.com/controller_name/funtion_name");
// Building post parameters
// key and value pair
List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(4);
nameValuePair.add(new BasicNameValuePair("imei", "12345"));
nameValuePair.add(new BasicNameValuePair("welawa", "12315646545"));
nameValuePair.add(new BasicNameValuePair("lat", "123.25"));
nameValuePair.add(new BasicNameValuePair("long", "52.22323"));
// 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();
}
}
@Override
protected Boolean doInBackground(Void... params) {
// TODO Auto-generated method stub
return null;
}
}
I am a php developer, trying to write a simple app that logs GPS cordinates. 我是一名PHP开发人员,试图编写一个记录GPS坐标的简单应用程序。
I can't check the console because some thing has made the emulator working. 我无法检查控制台,因为某些事情使仿真器正常工作。 I always get the emulator-arm.exe has stopped working bla bla bla.
我总是得到模拟器arm.exe停止工作。 So I use my phone and restart it everytime when I test.
因此,我使用手机并在每次测试时都重新启动它。 So unfortunately I do not have access to a log either.
因此,很遗憾,我也无法访问日志。
Any help is greatly appreciated. 任何帮助是极大的赞赏。
I don't know where is sendinfo()
called? 我不知道
sendinfo()
在哪里调用? But I think in your main Activity you call that: 但是我认为在您的主要活动中您可以这样称呼:
new SendData().sendInfo();
:( :(
Please put sendInfo to doInBackground 请将sendInfo放入doInBackground
And in your Activity : new SendData().execute();
在您的Activity中 :
new SendData().execute();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.