php - 保存数据错误HttpPost android

标签 php android mysql eclipse

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

然后我决定使其在线,现在我必须将程序中的整个 url 更改为 ' 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 是我的网站,我只是将其设置为 static ' http://kun-bfashion.com ,reqURL 就像“cek_user”。我使用该代码进行登录、从服务器获取数据等等。

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

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“保存数据问题”,但在我的离线数据库(本地主机)之前,我可以插入帖子并获取而不会出现错误。

这是我按下注册按钮时的 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

抱歉,帖子很长,但我不知道如何解释我的问题-_-。感谢您的阅读。

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

最佳答案

这可能是服务器问题,但根据 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 );

在浏览器中测试 URL,看看是否收到任何 json 响应。

关于php - 保存数据错误HttpPost android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29056226/

相关文章:

java - Android下载管理器下载位置

php - 在函数的 Try Catch 部分之后,函数中的参数值变为空字符串

php - WordPress 仪表板显示错误

php - 在后台搜索产品,例如输入 >=3

Android GestureDetector onScroll 意外行为

通过 TOAD 导入 MySql 在插入期间提示输入变量

java - 在 Android Studio 中将数据从 MySQL 显示到 TextView

php - 防止在 php 中多次出现相同的数组

php - SQL 从一个表中选择项目,并使用另一个表中的条件

android - 插入现有记录时 Android Realm copyToRealmOrUpdate 出错