我正在做一个android-php-mysql连接。我试过这个:
HttpClient httpclient = new DefaultHttpClient();
HttpGet request = new HttpGet();
request.setURI(new URI("http://iamjavadeveloper.com/NoTitle.php"));
HttpResponse response = httpclient.execute(request);
HttpEntity entity = response.getEntity();
并且php文件是:
$result=mysql_query("select * from titles");
while($row= mysql_fetch_assoc($result))
{
$output[]=$row;
}
echo json_encode($output,JSON_UNESCAPED_UNICODE);
但这不起作用;
php:
$result=mysql_query("select * from titles where id='$id' " );
while($row= mysql_fetch_assoc($result))
{
$output[]=$row;
}
echo json_encode($output,JSON_UNESCAPED_UNICODE);
和java:
HttpClient httpclient = new DefaultHttpClient();
HttpGet request = new HttpGet();
request.setURI(new URI("http://iamjavadeveloper.com/NoTitle.php?id=23"));
HttpResponse response = httpclient.execute(request);
和Logcat:
03-20 00:50:41.939: E/AndroidRuntime(2758): FATAL EXCEPTION: AsyncTask #1
03-20 00:50:41.939: E/AndroidRuntime(2758): Process: com.example.onur, PID: 2758
03-20 00:50:41.939: E/AndroidRuntime(2758): java.lang.RuntimeException: An error occured while executing doInBackground()
03-20 00:50:41.939: E/AndroidRuntime(2758): at android.os.AsyncTask$3.done(AsyncTask.java:300)
03-20 00:50:41.939: E/AndroidRuntime(2758): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
03-20 00:50:41.939: E/AndroidRuntime(2758): at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
03-20 00:50:41.939: E/AndroidRuntime(2758): at java.util.concurrent.FutureTask.run(FutureTask.java:242)
03-20 00:50:41.939: E/AndroidRuntime(2758): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
03-20 00:50:41.939: E/AndroidRuntime(2758): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
03-20 00:50:41.939: E/AndroidRuntime(2758): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
03-20 00:50:41.939: E/AndroidRuntime(2758): at java.lang.Thread.run(Thread.java:818)
03-20 00:50:41.939: E/AndroidRuntime(2758): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
03-20 00:50:41.939: E/AndroidRuntime(2758): at android.os.Handler.<init>(Handler.java:200)
03-20 00:50:41.939: E/AndroidRuntime(2758): at android.os.Handler.<init>(Handler.java:114)
03-20 00:50:41.939: E/AndroidRuntime(2758): at android.widget.Toast$TN.<init>(Toast.java:336)
03-20 00:50:41.939: E/AndroidRuntime(2758): at android.widget.Toast.<init>(Toast.java:100)
03-20 00:50:41.939: E/AndroidRuntime(2758): at android.widget.Toast.makeText(Toast.java:250)
03-20 00:50:41.939: E/AndroidRuntime(2758): at com.example.onur.MainActivity$MarkOnMap.doInBackground(MainActivity.java:415)
03-20 00:50:41.939: E/AndroidRuntime(2758): at com.example.onur.MainActivity$MarkOnMap.doInBackground(MainActivity.java:1)
03-20 00:50:41.939: E/AndroidRuntime(2758): at android.os.AsyncTask$2.call(AsyncTask.java:288)
03-20 00:50:41.939: E/AndroidRuntime(2758): at java.util.concurrent.FutureTask.run(FutureTask.java:237)
03-20 00:50:41.939: E/AndroidRuntime(2758): ... 4 more
答案 0 :(得分:0)
请发布详细代码。根据stacktrace,看起来你正试图从doInBackground()访问UI线程。
确保您没有访问任何UI代码,即doInBackGround()方法中的Toast。
答案 1 :(得分:0)
<强> MainActivity.class 强>
new ParserClass(new CalbackListener() {
@Override
public void onSuccess(String result) {
Toast.makeText(getApplicationContext(), result,
Toast.LENGTH_LONG).show();
}
}).execute();
您的Parser类
package com.example.soapsample;
import java.io.IOException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import android.os.AsyncTask;
import android.util.Log;
public class ParserClass extends AsyncTask<Void, Void, String> {
public interface CalbackListener {
public void onSuccess(String result);
}
private CalbackListener callback;
private String url = "http://iamjavadeveloper.com/NoTitle.php?id=23";
public ParserClass(CalbackListener callback1) {
this.callback = callback1;
}
@Override
protected String doInBackground(Void... params) {
String result = null;
try {
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
Log.i("url", url);
HttpResponse httpResponse = null;
if (httpGet != null) {
httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
result = EntityUtils.toString(httpEntity);
Log.i("Result", result);
}
return result;
} catch (ClientProtocolException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
callback.onSuccess(result);
}
}
它将返回一些值,确保您的网址并返回值。