请帮我找一个解决方案:
如果我调用URL(下面),服务器会提供类似这样的内容:
"Name", "Date" "Peter", "01.01.2013" "Paul", "12.12.2012"
所以服务器为我提供了一个SQL表作为文本。我试图请求它并将其转换为字符串。但是当我运行应用程序时,字符串是空的。
我是否对网址进行了编码? 我是否正确处理了请求?
以下是网址:
http://IP/query.html?sql="select * from ADAnreden"
这是由Log.d:
发布的URLhttp://IP/query.html?sql=%22select+*+from+ADAnreden%22
这是我的代码:
import java.io.BufferedInputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import android.util.Log;
public class Reader {
private String result = "leer";
String strUrl = "getURL: fehlgeschlagen";
private HttpURLConnection urlConnection;
public String getStringFromServer(){
String ip = "XYZ";
try {
String query = URLEncoder.encode("\"select * from ADAnreden\"", "utf-8");
strUrl = "http://"+ip+"/query.html?sql=" + query;
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.d(PACKAGE_NAME, "After getURL"+strUrl);
Thread thread = new Thread()
{
@Override
public void run() {
try {
URL url = new URL(strUrl);
urlConnection = (HttpURLConnection) url.openConnection();
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
result = convertStreamToString(in);
} catch (Exception e) {
e.printStackTrace();
} finally {
urlConnection.disconnect();
}
}
};
thread.start();
return result;
}
static String convertStreamToString(java.io.InputStream is) {
java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A");
return s.hasNext() ? s.next() : "";
}
}
编辑:
以下是浏览器的示例请求(在这种情况下只是行,所以没有,)
答案 0 :(得分:0)
你应该使用一个标准化的api来从你的web服务传输数据,这将节省你很多时间和沮丧解析你自己的响应,因为有像GSON这样的库...可以将JSON对象转换为JAVA对象,例如
使用asynctask类适合执行您尝试执行的操作。使用asynctask类解决您的问题的方法如下所示。
private void getString() {
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
} catch (Exception e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "UTF-8"));
sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result = sb.toString();
} catch (Exception e) {
e.printStackTrace();
}
//parse your data here
}
class getterThread extends AsyncTask<Void, Void, Void> {
private ProgressDialog progressDialog = new ProgressDialog(Ingolist.this);
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog.setMessage("Loading data from server...");
progressDialog.show();
progressDialog.setOnCancelListener(new OnCancelListener() {
@Override
public void onCancel(DialogInterface arg0) {
getterThread.this.cancel(true);
}
});
}
@Override
protected Void doInBackground(Void... params) {
getString();
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
progressDialog.dismiss();
// Update UI here
}
}