1

I keep getting a "null" exception when I run this code. If I manually type the url string into my browser the php script posts "{query_result:SUCCESS}" indicating it worked, but when I try it via this code I just get the Exception e as "null". Can anyone help?

protected String signUp(String username) {
        String link = "http://12.34.56.78/JSON_addUser.php";
        String data = "";
        BufferedReader bufferedReader;
        String result = "";
        String message;

        try {
            data += "?username=" + URLEncoder.encode(username, "UTF-8");
            link += data;

            URL url = new URL(link);

            HttpURLConnection con = (HttpURLConnection) url.openConnection();
            bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));
            result = bufferedReader.readLine();
            return result;
        } catch (Exception e) {
            message = "Exception trying:  " + link + "   Got: " + e.getMessage() + result;
            return message;
        }
    }
Jason C
  • 38,729
  • 14
  • 126
  • 182
pandron
  • 43
  • 1
  • 8
  • 2
    What line does the exception occur on? Please generate and show the whole stack trace. I'm sparing you the dupe of http://stackoverflow.com/q/218384/616460 for the time being. – Jason C Mar 03 '17 at 21:55
  • 1
    can you provide the error log please – Ousmane D. Mar 03 '17 at 21:56
  • 1
    ^ Making sure that you modify your code to dump the exception to the error log first. – Jason C Mar 03 '17 at 21:56
  • I'm actually not getting an error at all. I just get a "null" response from the JSON_addUser.php when I query via the Android app, but when I manually type in the exact link the app is using it works. – pandron Mar 04 '17 at 21:17

1 Answers1

0

Pretty often servers block requests like this because they are automated without a "User Agent" header. You might want to behave like a "real" browser.

Also i see a content encoding issue here. The constructor from InputStreamReader uses the default encoding and this might not be the encoding from the server--it might crash from converting the bytes into chars.

Christian Ullenboom
  • 1,388
  • 3
  • 24
  • 20