gpt4 book ai didi

java - Android 应用程序向服务器发送数据时崩溃

转载 作者:行者123 更新时间:2023-12-01 23:14:54 25 4
gpt4 key购买 nike

我正在尝试将一些数据发送到我的服务器,该服务器在互联网上公开可用。无论应用程序如何崩溃,应用程序都会在启动完成(添加权限)后作为服务启动。我已经缩小了代码范围。当它尝试向服务器发送数据时,它似乎崩溃了。

下面是我的代码。如果你们能在这方面帮助我,我真的很感激。

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;
}




}

我是一名 PHP 开发人员,正在尝试编写一个记录 GPS 坐标的简单应用程序。

我无法检查控制台,因为某些事情使模拟器正常工作。我总是得到 emulator-arm.exe 已停止工作 bla bla bla。所以我每次测试时都会使用手机并重新启动它。不幸的是,我也无法访问日志。

非常感谢任何帮助。

最佳答案

我不知道sendinfo()在哪里调用?但我认为在你的主要 Activity 中你称之为:

new SendData().sendInfo(); :(

请将sendInfo放入doInBackground

在您的Activity中:new SendData().execute();

关于java - Android 应用程序向服务器发送数据时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21399093/

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