0

我构建了一个简单的应用程序,它使用 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;
}
}
4

0 回答 0