我构建了一个简单的应用程序,它使用HTTP POST请求从远程数据库获取数据。我决定改变它的布局,但现在我意识到的请求在真实设备上不起作用。
我只需更改布局并在此新项目中复制相同的类。我无法理解什么是错的。当我使用该应用程序时,它会在启动画面活动中停止并等待。我查看了代码,我注意到我在执行POST请求时停止了。 当我在仿真器上执行项目时,它可以很好地工作。
这是我的班级:
public class MyHttpPostRequest extends AsyncTask<String, Void, String[]> {
private String link;
private String querySql;
public MyHttpPostRequest(String remoteLink, String richiestaSql) {
this.link = remoteLink;
this.querySql = richiestaSql;
}
@Override
protected String[] doInBackground(String... params) {
// Create a new HttpClient and Post Header
HttpClient httpClient = new DefaultHttpClient();
HttpPost request = new HttpPost(this.link);
BufferedReader bufferedReader = null;
List<NameValuePair> postParameters = new ArrayList<NameValuePair>();
postParameters.add(new BasicNameValuePair("query", this.querySql));
StringBuffer stringBuffer = null;
String[] token = new String[600];
try {
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(postParameters);
request.setEntity(entity);
HttpResponse response= httpClient.execute(request);
bufferedReader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
stringBuffer = new StringBuffer("");
String line = "";
String LineSeparator = System.getProperty("line.separator");
int i=0;
while ((line = bufferedReader.readLine()) != null) {
stringBuffer.append(line + LineSeparator);
token[i] = line + LineSeparator;
i++;
}
bufferedReader.close();
} catch(Exception e){
}
return token;
}
@Override
protected void onPostExecute(String[] result) {
super.onPostExecute(result);
}
public void eseguiRichiesta(){
this.execute(this.link, this.querySql);
}
public void setValues(String remoteLink, String richiestaSql){
this.link = remoteLink;
this.querySql = richiestaSql;
}
public void setLink(String link){
this.link = link;
}
public void setRequest(String requestSQL){
this.querySql = requestSQL;
}
}