0

before, I have database MySQL offline in my phpmyadmin (i using xampp). I can get and post(insert) request from my android device or emulator. The url is simple, like 'localhost/cek_user' oh, i using json for parsing data to php.

and then I decide to make it online, now I must change the whole url in my program to 'http://kun-bfashion.com/cek_user' the problem is in here.

I can get like username, password, and more with this code, I just call it in my class:

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 and reqUrl is my site, I just setting it to static 'http://kun-bfashion.com, and reqURL is like 'cek_user'. i use that code for login, get data from server and many more.

but when i want to post like registration user, its showing toast "save data problem", i use this code for post to web service

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;
}

The Error is showing Toast "Save Data Problem", but before in my offline database (localhost) i can insert post and get without no error.

this is my LogCat when i press registrastion button:

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

sorry for long post, but i don't know how to explain my problem -_-. thanks for read.

The problem is i can get from server, but i can't post

  • Have you added internet permission in manifest? – Fahim Mar 15 '15 at 01:28
  • of course i have added internet permission. if no i can't get username or password for login. i can get but can't post.. – Xenix Putra Sasongko Mar 15 '15 at 01:29
  • Make sure your server's firewall has been properly configured. If the server is running on your own computer, you will need to look into port forwarding. – James Wong Mar 15 '15 at 01:34
  • The server is not running in my computer, its running in somewhere i don't know. because i just hosting it. i think the firewall is configured. because i can get username and password. but i can't post.. – Xenix Putra Sasongko Mar 15 '15 at 01:39

1 Answers1

0

It could be a server issue, but according to This post the issue might be solved by changing your code to apply the HttpParams to the HttpPost instead of the 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));

Edit: Validate the url with logging:

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

Test the url in a browser and see if you get any json response.

Community
  • 1
  • 1
Daniel Nugent
  • 43,104
  • 15
  • 109
  • 137
  • i have try your code and your link, but still not working.. i can get like username and password for login. but i can't post like registration or something like that -_- – Xenix Putra Sasongko Mar 15 '15 at 15:52
  • Hmm, it must be something server side, or the url then, since it worked using xampp. Use logging to verify that the url is correct as a next step. – Daniel Nugent Mar 15 '15 at 15:57