我是Java和Android开发的新手,并尝试创建一个简单的应用程序,它应该联系Web服务器并使用http get将一些数据添加到数据库.
当我使用计算机中的Web浏览器进行呼叫时,它可以正常工作.但是,当我在Android模拟器中执行运行应用程序的调用时,不会添加任何数据.
我已将Internet权限添加到应用程序的清单中.Logcat不会报告任何问题.
任何人都可以帮我弄清楚什么是错的?
这是源代码:
package com.example.httptest;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class HttpTestActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
setContentView(tv);
try {
URL url = new URL("http://www.mysite.se/index.asp?data=99");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.disconnect();
tv.setText("Hello!");
}
catch (MalformedURLException ex) {
Log.e("httptest",Log.getStackTraceString(ex));
}
catch (IOException ex) {
Log.e("httptest",Log.getStackTraceString(ex));
}
}
}
Run Code Online (Sandbox Code Playgroud) 尝试使用PHP将我的域服务器中的MySQL数据显示到Android应用程序并将我的数据显示为Toast消息.每当我运行应用程序时,我都会空着吐司.此外,我将使用列表视图来显示数据.代码中没有错误.下面是我的AsychTask的代码:
class Connection extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... params) {
String result = "";
String host = "http://prasaurus.com/conn.php";
try {
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet();
request.setURI(new URI(host));
HttpResponse response = client.execute(request);
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer stringBuffer = new StringBuffer("");
String line = "";
while ((line = reader.readLine()) != null ){
stringBuffer.append(line);
break;
}
reader.close();
result = stringBuffer.toString();
} catch (URISyntaxException e) {
e.printStackTrace();
} catch (ClientProtocolException e) { …
Run Code Online (Sandbox Code Playgroud)