0

My app has been using the same code for over a years where it send an email address and a password to my JSON web services site and then retrieves the given information. I'm now trying to send some extra information for updating purposes but when I add the two extra values the app crashes.

private void doTfr() {
  String result = "";
  String mailAddr = " ";
  String feedback = " ";
  String lectName = MyLecturesActivity.LectName;
  String url = "http://jobtracker.myweb.com/jsonWeb/Default.aspx";

  mailAddr = "s.chase@testweb.com";     
  EditText fBack = (EditText) findViewById(R.id.myfeedback_feedback2);
  feedback = fBack.getText().toString();
  lectName = "Test Android";

  url = url + "?maddr=" + mailAddr + "&pwd=FB&lect=" + "\"lectName\"&fb=\"" + feedback + "\""; 

  //url = url + "?maddr=" + mailAddr + &pwd=BI"; Using this url works!                    
  try {

    HttpClient httpclient = new DefaultHttpClient();

        HttpGet httpget = new HttpGet(url);   ****Crashes here

        HttpResponse response;

        response = httpclient.execute(httpget);
        HttpEntity entity = response.getEntity();

        if (entity != null) {

          InputStream instream = entity.getContent();
          result = convertStreamToString(instream);
          result = result.replaceAll(""", "\"");

          JSONObject json=new JSONObject(result);
          JSONArray nameArray=json.names();
          JSONArray valArray=json.toJSONArray(nameArray);
        endif
          nstream.close();
        }


    } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
    } catch (IOException e) {
           // TODO Auto-generated catch block
            e.printStackTrace();
    } catch (JSONException e) {
        }
      }
}

When I use the commented out url it works. Can someone tell me why adding the two new values causes it to crash, could it be the size of the string?

user616076
  • 3,907
  • 8
  • 38
  • 64
  • can you print the logcat for error scenario – curious Dec 09 '12 at 13:10
  • Why are there quotation marks in your url for the GET request? – se_bastiaan Dec 09 '12 at 14:47
  • I've since removed the quotation marks and it has made no difference. The output from Logcat probably wont help but here it is E/AndroidRuntime( 638): at java.lang.reflect.Method.invokeNative(Native Method) – user616076 Dec 09 '12 at 14:55
  • And..E/AndroidRuntime( 638): at java.lang.reflect.Method.invoke(Method.java:521) E/AndroidRuntime( 638): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) E/AndroidRuntime( 638): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) E/AndroidRuntime( 638): at dalvik.system.NativeStart.main(Native Method) – user616076 Dec 09 '12 at 14:56
  • And..W/ActivityManager( 59): Force finishing activity org.HG_Feedback/.FeedbackActivity W/ActivityManager( 59): Activity pause timeout for HistoryRecord{45047fd0 – user616076 Dec 09 '12 at 14:57
  • add exception in question. **do not** add in the comments – Mohsin Naeem Dec 09 '12 at 15:28

1 Answers1

0

You'd need to encode that URL to handle quotation marks.

String encodedUrl = URLEncoder.encode(url, "UTF-8");
kgu87
  • 2,050
  • 14
  • 12
  • Thank you for your help with this, I tried your suggestion and now I get past the httpGet but it crashes a few lines later on the line with response = httpclient.execute(httpget); The message is Target host must not be null, or set in parameters. If I don't use encode and use the plain url it crashes with the message Illegal character in query at index 93 and then adds the url which if I use in a browser works fine! Any help much appreciated – user616076 Dec 09 '12 at 14:32
  • url = url + "?maddr=" + mailAddr + "&pwd=FB&lect=" + URLEncoder.encode("\"" + lectName + "\"", "UTF-8") + "&fb=" + URLEncoder.encode("\"" + feedback + "\"", "UTF-8"); – kgu87 Dec 09 '12 at 14:44