0

I build an application that consumes JSON data, and I did a test on android device APIs 16, I use a library 'okhttp' to connect to the server, when I use the API 16 all smoothly, but when I use the API 17 and 19, data can not be displayed and only display the error log as follows:

04-19 16:17:38.394  14379-14411/com.example.mnafian.gambar E/Err IO﹕ java.net.UnknownHostException: http://

This is an example of code that I made to retrieve the data:

package com.example.mnafian.gambar;

import android.media.Image;
import android.os.AsyncTask;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.HashMap;


public class MainActivity extends ActionBarActivity {

    TextView tv1, tv2;
    OkHttpClient clientRest = new OkHttpClient();
    String list[] = new String[100];

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv1 = (TextView) findViewById(R.id.header_view);
        tv2 = (TextView) findViewById(R.id.detail_view);

        new LoadText().execute();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    public class LoadText extends
            AsyncTask<String, Void, String[]> {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();

        }

        @Override
        protected String[] doInBackground(String... params) {
            String list[] = new String[100];
            try {
                String url = "http://www.samplesite.net/omahnyewo/servicelist.php?type=kontrakan&lat=-7.9425479&long=112.6043546";

                Request request = new Request.Builder().url(url)
                        .build();

                Response response = clientRest.newCall(request).execute();
                String responsedata = response.body().string();
                JSONArray callData = new JSONArray(responsedata);

                if (callData != null) {
                    for (int i = 0; i < callData.length(); i++) {
                        JSONObject objData = callData.getJSONObject(i);
                        String tv1 = objData.getString("nama");
                        String tv2 = objData.getString("harga");
                        list[0] = tv1;
                        list[1] = tv2;
                    }
                }


            } catch (JSONException eex) {
                Log.e("Err JSON", eex.toString());
            } catch (IOException eex) {
                Log.e("Err IO", eex.toString());
            }
            return list;
        }

        @Override
        protected void onPostExecute(String[] strings) {
            super.onPostExecute(strings);
            list = strings;

            if (list.length>0){
                tv1.setText(list[0]);
                tv2.setText(list[1]);
            }
        }
    }
}

no data is received with the above code, I use the library with version 2.2.0 okhttp.

Is there an error in the code that I created?

1 Answers1

1

From your error log, we can infer that the host of API you try to access is can't be identified/reached(probably dead). Probably there is problem in your internet connection or the DNS server. But, if you are using local machine please, read this, and this. My advice is, first, try to check using internal browser to check the URI you use.

Community
  • 1
  • 1
gamalan
  • 123
  • 10