1

i'm try to get json file from mvc 4 application using webapi but i get an error in asynctask doinbackground method

java.lang.illegalargumentexception host may not be null

when i track my code i found that it crash on jsonparser in getjson from url at the last line of this code

public JSONObject getJSONFromUrl(String Url) throws JSONException
    {

        try {
            Log.d("tony","6"+Url);
            DefaultHttpClient httpClient = new DefaultHttpClient(); // to connect to http
            Log.d("tony","7"+Url);
            HttpGet httpGet=new HttpGet(Url);
            Log.d("tony","8"+httpGet.getMethod());
            HttpResponse httpResponse = httpClient.execute(httpGet);

and this is my do in background code

 public JSONObject doInBackground(String... urls) {
  // get url pointing to entry point of API
  String address = urls[0].toString();
  if (method.equals(LOAD_REQUEST)) 
  {
    return getJSON(address);

  }
  else
   return makeHttpRequest(address, method, parameters);
 }

the get json method

public JSONObject getJSON(String url){
       JSONObject j = null;
    try {
         j=parser.getJSONFromUrl(url);
    } catch (JSONException e) {

        e.printStackTrace();
    }
    return j;
 }
3
  • u have added intermnet permission? Commented Aug 7, 2014 at 18:28
  • yes i added internet permission Commented Aug 7, 2014 at 18:29
  • localhost:2100/api/products Commented Aug 7, 2014 at 18:29

3 Answers 3

1

change

 localhost:2100/api/products

to

http://localhost:2100/api/products
Sign up to request clarification or add additional context in comments.

Comments

1

first correction :

localhost:2100/api/products

to

http://localhost:2100/api/products

Second, change

http://localhost:2100/api/products

to

http://ipaddress:2100/api/products

Because if you are using "localhost" you are pointing to the device host, and you need point the server.

Comments

1

you need to add http:// to the url like that:

http://localhost:2100/api/products

1 Comment

are you friend of Tony ?

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.