保存数据错误HttpPost android

时间:2015-03-15 01:22:31

标签: php android mysql eclipse

之前,我的phpmyadmin中有数据库MySQL离线(我使用的是xampp)。我可以从我的Android设备或模拟器获取并发布(插入)请求。网址很简单,例如localhost / cek_user'哦,我使用json将数据解析为php。

然后我决定将其设为在线,现在我必须将我的程序中的整个网址更改为' http://kun-bfashion.com/cek_user'问题出在这里。

我可以使用此代码获得用户名,密码等内容,我只需在我的课程中调用它:

public String sendGetRequest(String reqUrl){
    HttpClient httpClient;
    HttpGet httpGet = new HttpGet(serverUri+"/"+reqUrl);
    InputStream is = null;
    StringBuilder stringBuilder = new StringBuilder();
    try {
        HttpParams params = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(params, 3000);
        HttpConnectionParams.setSoTimeout(params, 3000);
        httpClient = new DefaultHttpClient(params);
        Log.d(TAG, "executing...");
        HttpResponse httpResponse = httpClient.execute(httpGet);
        StatusLine status = httpResponse.getStatusLine();
        if(status.getStatusCode() == HttpStatus.SC_OK && httpResponse != null){
            /* mengambil response string dari server     */
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();
            BufferedReader reader = new BufferedReader(new InputStreamReader(is));
            String line = null;
            while((line = reader.readLine()) != null){
                stringBuilder.append(line+"\n");
            }
            is.close();
        }
    } catch (Exception e) {
        //Log.d(TAG, e.getMessage());
    }
    return stringBuilder.toString();
}

serverUri和reqUrl是我的网站,我只是将其设置为静态' http://kun-bfashion.com,而reqURL就像是' cek_user'。我使用该代码进行登录,从服务器获取数据等等。

但是当我想发布像注册用户一样,它显示吐司"保存数据问题",我使用此代码发布到网络服务

public int sendPostRequest(User user, String url){
    int replyCode = 99;
    HttpClient httpClient;
    HttpPost post = new HttpPost(this.serverUri+"/"+url);
    List<NameValuePair> value = new ArrayList<NameValuePair>();
    /* menambahkan parameter ke dalam request */
    value.add(new BasicNameValuePair("id_user", user.getId_User().toString()));
    value.add(new BasicNameValuePair("username", user.getUsername()));
    value.add(new BasicNameValuePair("nama_user", user.getNamaUser()));
    value.add(new BasicNameValuePair("email_user", user.getEmailUser()));
    value.add(new BasicNameValuePair("password_user",user.getPasswordUser()));

    try {
        HttpParams params = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(params, 3000);
        HttpConnectionParams.setSoTimeout(params, 3000);
        httpClient = new DefaultHttpClient(params);
        post.setEntity(new UrlEncodedFormEntity(value));
        Log.d(TAG, "executing post...");
        HttpResponse httpResponse = httpClient.execute(post);
        StatusLine status = httpResponse.getStatusLine();
        if(status.getStatusCode() == HttpStatus.SC_OK){
            Log.d(TAG, "submitted sucessfully...");
            replyCode = status.getStatusCode();
        }
    } catch (IOException e) {
        Log.d(TAG, e.getMessage());
    }
    return replyCode;
}

错误显示Toast&#34;保存数据问题&#34;,但在我的离线数据库(localhost)之前,我可以插入帖子并获得没有错误。

当我按下注册按钮时,这是我的LogCat:

03-15 08:19:33.032: I/System.out(23214): ex:org.apache.http.NoHttpResponseException: The target server failed to respond
03-15 08:19:33.032: I/System.out(23214): retry3
03-15 08:19:33.033: I/System.out(23214): [socket][13] connection    /66.85.138.130:80;LocalPort=53906(3000)
03-15 08:19:33.033: I/System.out(23214): [CDS]connect[/66.85.138.130:80] tm:3
03-15 08:19:33.034: D/Posix(23214): [Posix_connect Debug]Process com.koenb_fashion_fix :80 
03-15 08:19:33.056: I/System.out(23214): [socket][/192.168.1.111:53906] connected
03-15 08:19:33.056: I/System.out(23214): [CDS]rx timeout:3000
03-15 08:19:33.056: I/System.out(23214): >doSendRequest
03-15 08:19:33.058: I/System.out(23214): <doSendRequest
03-15 08:19:33.331: I/SurfaceTextureClient(23214): [STC::queueBuffer] (this:0x53ecad00) fps:50.80, dur:1023.54, max:43.97, min:3.79
03-15 08:19:33.505: I/SurfaceTextureClient(23214): [STC::queueBuffer] (this:0x53e75dd0) fps:1.97, dur:1015.42, max:508.04, min:507.38
03-15 08:19:33.523: I/System.out(23214): [CDS]close[53906]
03-15 08:19:33.524: I/System.out(23214): close [socket][/0.0.0.0:53906]
03-15 08:19:33.538: I/System.out(23214):    ex:org.apache.http.NoHttpResponseException: The target server failed to respond
03-15 08:19:33.539: I/System.out(23214): retry4
03-15 08:19:33.539: I/System.out(23214): close [socket][/0.0.0.0:53906]
03-15 08:19:33.540: I/System.out(23214): close [socket][/0.0.0.0:53906]
03-15 08:19:33.540: D/ServerRequest(23214): The target server failed to respond
很抱歉很长的帖子,但我不知道如何解释我的问题-_-。谢谢你的阅读。

问题是我可以从服务器获取,但我无法发布

1 个答案:

答案 0 :(得分:0)

这可能是服务器问题,但根据This post,可能会通过更改代码以将HttpParams应用于HttpPost而不是DefaultHttpClient来解决问题:

    HttpParams params = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(params, 3000);
    HttpConnectionParams.setSoTimeout(params, 3000);
    //httpClient = new DefaultHttpClient(params);
    httpClient = new DefaultHttpClient(); //use default constructor instead
    post.setParams(params); //add call to setParams on HttpPost
    post.setEntity(new UrlEncodedFormEntity(value));

编辑: 使用日志记录验证URL:

HttpPost post = new HttpPost(this.serverUri+"/"+url);
Log.d(TAG, "post url: " + this.serverUri+"/"+url );

在浏览器中测试网址,看看是否有任何json响应。