1

I am working on an app that makes an API call to a php script that echos a JSON Object. Testing the php file manually through a browser returns the expected information, but my app is acting as if the string that is returned is empty (before I even get to the point of decoding the JSON Object).

Here's the snippet of my code. I've used this script multiple times in my app successfully for api's that echo strings.

String urlParameters = 
    "request=item_search&item_num=" + barcode + "&ou=" + OU + "&user_tag=" + initials + "&version=" + version + "&scan_point=return";
URL url = null;
try {
        if (testMode == true) 
        {
            url = new URL("http://URL/api.php");
        } 
        else 
        {
            url = new URL("http://URL/api.php");
        }
    } 
    catch (MalformedURLException e) 
    {
        e.printStackTrace();
    }
    StringBuilder output = new StringBuilder();
    try 
    {
        assert url != null;
        URLConnection conn = url.openConnection();
        conn.setDoOutput(true);
        OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream());
        writer.write(urlParameters);
        writer.flush();
        writer.close();
        String line;
        BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        while ((line = reader.readLine()) != null)
        {
            output.append(line);
        }
        writer.close();
        reader.close();
    } 
    catch (IOException e) 
    {
        e.printStackTrace();
    }
    String outputString = output.toString();
5
  • if you see and get github.com/ErNaveen/HorizontalListview_Example/blob/master/app/… Commented Sep 16, 2015 at 12:44
  • Unfortunately, that app is using a defaultHttpClient class, which is deprecated. However, looking over the code, we are still reading from the URL the same way. I have no idea why my string for some reason is empty, however. Commented Sep 16, 2015 at 12:47
  • well if your sever is running the latest version of apache and php. Sending it a depreciated header will give you nothing as you have found out. Is it possible to get your app to send a different header, as supposed to the depreciated default? Commented Sep 16, 2015 at 12:59
  • As I stated before the URLConnection class is working fine for getting responses from the API that are in the form of strings (I use it for pretty much everything else in my app). The problem is that for this particular feature I'm implementing, I have to use someone else's API (still running on the same server) that echos back a JSON Object, rather than a string, but for some reason the string is coming back as empty before it even has a chance to be parsed. Commented Sep 16, 2015 at 13:05
  • @KGillis I have posted My Answer, You can check it. Commented Sep 16, 2015 at 13:11

1 Answer 1

2

Have you tried OkHttp.

HTTP is the way modern applications network. It’s how we exchange data & media. Doing HTTP efficiently makes your stuff load faster and saves bandwidth.

You can try following code:

package com.squareup.okhttp.guide;

import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import java.io.IOException;

public class GetExample {
  OkHttpClient client = new OkHttpClient();

  String run(String url) throws IOException {
    Request request = new Request.Builder()
        .url(url)
        .build();

    Response response = client.newCall(request).execute();
    return response.body().string();
  }

  public static void main(String[] args) throws IOException {
    GetExample example = new GetExample();
    String response = example.run("https://raw.github.com/square/okhttp/master/README.md");
    System.out.println(response);
  }
}

For more you can visit:

Vogella's article

OkHttp 2.0

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.