0

Hi,

i use this class to make a request to server, which consist of the json data object. Class is:-

public class HttpClient {

private static String URL = "localhost/json/json_handle.php";

public String postJsonData(String data) {

    try {
        StringBuffer buffer = new StringBuffer();
        // Apache HTTP Reqeust
        System.out.println("Sending data..");
        System.out.println("Data: [" + data + "]");
        org.apache.http.client.HttpClient client = new DefaultHttpClient();
        HttpPost post = new HttpPost(URL);
        List<NameValuePair> nvList = new ArrayList<NameValuePair>();
        BasicNameValuePair bnvp = new BasicNameValuePair("json", data.toString());
        // We can add more

        nvList.add(bnvp);
        post.setEntity(new UrlEncodedFormEntity(nvList));

        HttpResponse resp = client.execute(post);
        // We read the response
        InputStream is = resp.getEntity().getContent();
        BufferedReader reader = new BufferedReader(
                new InputStreamReader(is));
        StringBuilder str = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            str.append(line + "\n");
        }
        is.close();
        buffer.append(str.toString());
        // Done!

        return buffer.toString();
    } catch (Throwable t) {
        t.printStackTrace();
    }

    return null;
}
}

Then i use a php class on server side to get the json object from the request. But, at server side i am getting nothing. Even when i use $_REQUEST method, then code after this method doesn't work.

Here is my php file:-

    <?php
    $file = fopen("MyFile.txt" ,"w");
    $int = $_REQUEST;
    fwrite($file,"aaa");

    //$input =$_REQUEST['json'];
    fwrite($file,"HELLO  111");

    //$data = json_decode($input,true);

    /*print_r($input);
    // get values
    $firstname = $input->firstName;
    $surename = $input->lastName;
    $age = intval($input->age);

    // check values
    if (isset($firstname) && !empty($firstname) && 
        isset($surename) && !empty($surename) &&
        isset($age) && is_numeric($age))
    {
        // do something
        echo "Hello ".htmlspecialchars($firstname)." ".htmlspecialchars($surename)."!<br>";
        echo "You are $age years old! Wow.";
    }
    else
    {
        echo "Some values are missing or incorrect";
    }*/

    //fwrite($file, $data);

    fclose($file);

?>

Any suggestions regarding this problem???

codemania
  • 1,098
  • 1
  • 9
  • 26
Iqbal Singh
  • 95
  • 1
  • 10
  • what is your mean about doesn't work? – Shayan Pourvatan Mar 03 '14 at 09:17
  • Do you tried calling your php manually with `$_GET` params and checked if the logging works? – Tobias Golbs Mar 03 '14 at 09:18
  • I use file handling in php file. After $int = $_REQUEST; line my code doesn't work that is it not create any file. That, means it is giving an error. – Iqbal Singh Mar 03 '14 at 09:20
  • why you are doing $int = $_REQUEST? Try to run your code with ini_set("display_errors", 1). This should give you the error that is causing this behaviour. OR you are getting Response code 500 in your client code? – Purusottam Kaushik Mar 03 '14 at 09:24
  • @PurusottamKaushik i am just checking that is my request is coming or not. And also when i use ini_set("display_errors",1) then also my code didn't generate file on server. – Iqbal Singh Mar 03 '14 at 09:29
  • @PurusottamKaushik when i execute php on server then it shows " Undefined index: json in C:\wamp\www\json\json_handle.php " in the browser. – Iqbal Singh Mar 03 '14 at 09:47
  • ini_set("display_errors",1) will only display errors on page. Undefined index: json mean that $_REQUEST doesn't have JSON as parameter. Are you sure this line is commented in your code? – Purusottam Kaushik Mar 03 '14 at 09:55
  • @PurusottamKaushik i just check this by uncommenting this line and then i got the error which i tell you. But, as u can see that i mention 'json' when send post request as you can see in my java file. – Iqbal Singh Mar 03 '14 at 10:00
  • OK IQBAL this mean that you are getting "JSON" variable in PHP $_REQUEST. Either it is not being passed from client. Are you sure post.setEntity(new UrlEncodedFormEntity(nvList)); is setting the request parameters properly? – Purusottam Kaushik Mar 03 '14 at 10:42
  • @PurusottamKaushik i also hv doubt here. Bcoz when i print the nvList then it gives me output as:- "json={all data is shown here}" So i think problem is in post.setEntity But, how i can check it???? – Iqbal Singh Mar 03 '14 at 10:46
  • you need to make sure parameters are being passed correctly. once it is confirmed then you look at php code. ALL THE BEST man. – Purusottam Kaushik Mar 03 '14 at 10:48
  • please review my answer her [Android JSON example](http://stackoverflow.com/questions/22053132/removing-linebreak-from-php-json-output/22053515#22053515 "Android JSON example") – Samy Massoud Mar 03 '14 at 09:25
  • your example is using manual data but i want to get data from the URL, which i am not able to get. – Iqbal Singh Mar 03 '14 at 09:31
  • when i execute php on server then it shows " Undefined index: json in C:\wamp\www\json\json_handle.php " in the browser. – Iqbal Singh Mar 03 '14 at 09:47
  • If you have a text file that you want to read in PHP and show it as a json for your android app, then you have to read file and explode it on lines , this will give you an array so you will need to echo it as a JSON encoded – Samy Massoud Mar 03 '14 at 09:52
  • i m still getting the array which is in json encoded form. JSON ENCODING part is DONE. Now, my problem is when i send that data through post request to php file then at php file i m not able to get the json object from the request and that you can check in my posted code. – Iqbal Singh Mar 03 '14 at 09:56
  • what is the output of `var_dump($_REQUEST));exit;` put it at the first of your file – Samy Massoud Mar 03 '14 at 10:11
  • I put that in my php file and execute it directly in browser and it give me this:- "array(0) { }" – Iqbal Singh Mar 03 '14 at 10:30
  • this is fine ,you have to call it from android and log the returned result , because if you call this from browser you will git nothing because you didn't send any data – Samy Massoud Mar 03 '14 at 10:50
  • i got this system error in the logcat :- "03-03 05:56:39.703: W/System.err(1896): java.lang.IllegalStateException: Target host must not be null, or set in parameters. scheme=null, host=null, path=localhost/json/json_handle.php " – Iqbal Singh Mar 03 '14 at 10:59
  • put `http://localhost/json/json_handle.php` and try again – Samy Massoud Mar 03 '14 at 12:04
  • I already try it and now i m getting this error in logcat "03-04 01:32:35.300: W/System.err(1092): org.apache.http.conn.HttpHostConnectException: Connection to http://localhost refused" – Iqbal Singh Mar 04 '14 at 06:38
  • You have to allow connection to your local host from local network and write your local host ip instead of localhost – Samy Massoud Mar 04 '14 at 08:34

1 Answers1

0

Thanks friends for your help. I got the solution and now the program is working perfectly on localhost as well as online.

For localhost, we just have to give the URL as:-

1.1.1.1/json/json_handle.php

where 1.1.1.1 is your ip address.

Again, thanks alot friends.

Community
  • 1
  • 1
Iqbal Singh
  • 95
  • 1
  • 10