我是Android编程的先驱,并试图通过开发应用程序来学习。 我想做的是将数据发送到网络服务器,网络服务器将进一步处理它。现在我早些时候使用 HttpClient 函数,但似乎已被弃用。有些研究把我带到了这个 HttpUrlConnect 类,它没有抛出任何错误,但也没有真正起作用! PhP没有收到这些数据。请帮忙!代码如下:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SendData sendData = new SendData();
Toast t4 = Toast.makeText(getApplicationContext(), "Started", Toast.LENGTH_LONG);
t4.show();
sendData.execute("21.43","23.23");
}
class SendData extends AsyncTask<String, Void, String>{
@Override
protected String doInBackground(String... params) {
// a-> Lat | b-> Lon
try {
String a_str = params[0];
String b_str = params[1];
String result = null;
URL url = null;
String text = "";
BufferedReader reader = null;
try {
String data = URLEncoder.encode("lon", "UTF-8") + "=" + URLEncoder.encode(a_str, "UTF-8");
data += "&" + URLEncoder.encode("lat", "UTF-8") + "=" + URLEncoder.encode(b_str, "UTF-8");
url = new URL("http://chalkbox.in/recieve.php");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("Host", "chalkbox.in");
try {
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(data);
wr.flush();
Toast t0 = Toast.makeText(getApplicationContext(), "Pushed", Toast.LENGTH_LONG);
t0.show();
} catch (Exception e) {
Toast t1 = Toast.makeText(getApplicationContext(), String.valueOf(e), Toast.LENGTH_LONG);
t1.show();
}
conn.connect();
} catch (Exception e) {
Toast t5 = Toast.makeText(getApplicationContext(), String.valueOf(e), Toast.LENGTH_LONG);
t5.show();
}
}
catch (Exception e){
Toast t11 = Toast.makeText(getApplicationContext(), String.valueOf(e), Toast.LENGTH_LONG);
t11.show();
}
return null;
}
}
}
现在,它只是为了“开始”而没有任何东西。我检查了互联网连接,互联网访问权限和一切。它不会推送数据,也不会出现任何错误。最终它停止了工作。
PhP代码是:
<?php
$long = $_POST['lon'];
$lati = $_POST['lat'];
$con = mysqli_connect($host",$user,$pass,$dB);
$sql = "INSERT INTO table (long, lati) VALUES('$long', '$lati')";
if(mysqli_query($con, $sql)){
mysqli_close($con);
}
?>
logcat的:
10-29 06:27:10.528 1919-1919 / com.employeetracker.theworkstation.attempt2我/艺术:不迟到-Xcheck:jni(已经开启)
10-29 06:27:10.849 1919-1941 / com.employeetracker.theworkstation.attempt2 D / OpenGLRenderer:使用EGL_SWAP_BEHAVIOR_PRESERVED:true
10-29 06:27:10.852 1919-1919 / com.employeetracker.theworkstation.attempt2 D /:HostConnection :: get()建立新主机连接0xb3eb3ef0,tid 1919
10-29 06:27:10.876 1919-1927 / com.employeetracker.theworkstation.attempt2 W / art:暂停所有线程:12.031ms
10-29 06:27:10.890 1919-1919 / com.employeetracker.theworkstation.attempt2 D / Atlas:验证地图......
10-29 06:27:10.919 1919-1941 / com.employeetracker.theworkstation.attempt2 D /:HostConnection :: get()建立新主机连接0xb3ffd110,tid 1941
10-29 06:27:10.959 1919-1941 / com.employeetracker.theworkstation.attempt2 I / OpenGLRenderer:初始化的EGL,版本1.4
10-29 06:27:11.009 1919-1941 / com.employeetracker.theworkstation.attempt2 D / OpenGLRenderer:启用调试模式0
10-29 06:27:11.036 1919-1941 / com.employeetracker.theworkstation.attempt2 W / EGL_emulation:eglSurfaceAttrib未实现
10-29 06:27:11.036 1919-1941 / com.employeetracker.theworkstation.attempt2 W / OpenGLRenderer:无法在表面0xb3f1d580上设置EGL_SWAP_BEHAVIOR,错误= EGL_SUCCESS
10-29 06:27:11.409 1919-1922 / com.employeetracker.theworkstation.attempt2 W / art:暂停所有线程:250.499ms
10-29 06:27:11.473 1919-1941 / com.employeetracker.theworkstation.attempt2 W / EGL_emulation:eglSurfaceAttrib未实现
10-29 06:27:11.474 1919-1941 / com.employeetracker.theworkstation.attempt2 W / OpenGLRenderer:无法在表面0xb3eabf80上设置EGL_SWAP_BEHAVIOR,错误= EGL_SUCCESS
10-29 06:27:11.566 1919-1927 / com.employeetracker.theworkstation.attempt2 I / art:背景部分并发标记扫描GC释放2566(119KB)AllocSpace对象,0(0B)LOS对象,50%免费,1000KB / 2024KB,暂停4.554ms总计147.763ms
10-29 06:27:11.588 1919-1927 / com.employeetracker.theworkstation.attempt2 W / art:暂停所有线程:11.688ms
10-29 06:27:11.669 1919-1919 / com.employeetracker.theworkstation.attempt2 I / Choreographer:跳过31帧!应用程序可能在其主线程上做了太多工作。
更新
10-30 01:39:39.374 10716-10731 / com.employeetracker.theworkstation.attempt2 E / AndroidRuntime:FATAL EXCEPTION:AsyncTask#1 过程:com.employeetracker.theworkstation.attempt2,PID:10716 java.lang.RuntimeException:执行doInBackground()
时发生错误在android.os.AsyncTask $ 3.done(AsyncTask.java:304)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
at android.os.Handler.<init>(Handler.java:200)
at android.os.Handler.<init>(Handler.java:114)
at android.widget.Toast$TN.<init>(Toast.java:344)
at android.widget.Toast.<init>(Toast.java:100)
at android.widget.Toast.makeText(Toast.java:258)
at com.employeetracker.theworkstation.attempt2.MainActivity.show(MainActivity.java:39)
at com.employeetracker.theworkstation.attempt2.MainActivity$SendData.doInBackground(MainActivity.java:80)
at com.employeetracker.theworkstation.attempt2.MainActivity$SendData.doInBackground(MainActivity.java:42)
at android.os.AsyncTask$2.call(AsyncTask.java:292)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
在android.os.AsyncTask $ SerialExecutor $ 1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor中$ Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818) 10-30 01:39:43.082 10716-10731 / com.employeetracker.theworkstation.attempt2 I / Process:发送信号。 PID:10716 SIG:9
答案 0 :(得分:0)
我完全改变了我的答案,因为现在问题很明显。 正如我在下面的评论中提到的,您无法在主线程上运行与服务器的连接(意味着您的活动或片段当前正在运行)。 需要做的是在异步类中运行它。按如下方式创建一个类:
class SendData extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
try{
//Your code
}
return null;
}
}
然后,您可以通过在您的activity / fragment中创建一个实例来调用此类,然后执行它。
您可以在this blog post上找到有关如何正确执行此操作的详细信息
答案 1 :(得分:0)
日志猫在Toast创作中显示错误 据我所知,您不能在主线程上进行UI操作 要在异步任务中显示Toast ,您必须调用 runOnUi 方法。