Confirm that your URL is right or not. I faced the same issue as my URL is http:// and i am accessing it as https://.
May be your URL is like :
// URL to get contacts JSON
private static String url = "http://123.xxx.x.xxx/DEMO/login_data.php";
Question
I am working on JSON data parsing, I am new to this as i am doing JSON parsing for the first time. I tried an example from This Link. It runs fine. But when i change the example code according to my need i got error. My changed code is as follows :
MainActivity.Java
public class MainActivity extends ListActivity {
private ProgressDialog pDialog;
// URL to get contacts JSON
private static String url = "https://123.xxx.x.xxx/DEMO/login_data.php";
// JSON Node names
private static final String TAG_STATUS = "Status";
private static final String TAG_USERNAME = "User Name";
private static final String TAG_PASSWORD = "Admin password";
// contacts JSONArray
JSONArray sampleData = null;
// Hashmap for ListView
ArrayList<HashMap<String, String>> userNamePass;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
userNamePass = new ArrayList<HashMap<String, String>>();
ListView lv = getListView();
// Listview on item click listener
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting values from selected ListItem
String name = ((TextView) view.findViewById(R.id.name))
.getText().toString();
String cost = ((TextView) view.findViewById(R.id.password))
.getText().toString();
// Starting single contact activity
Intent in = new Intent(getApplicationContext(),SingleContactActivity.class);
in.putExtra(TAG_USERNAME, name);
in.putExtra(TAG_PASSWORD, cost);
startActivity(in);
}
});
// Calling async task to get json
new GetContacts().execute();
}
/**
* Async task class to get json by making HTTP call
* */
private class GetContacts extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Please wait...");
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected Void doInBackground(Void... arg0) {
// Creating service handler class instance
ServiceHandler sh = new ServiceHandler();
// Making a request to url and getting response
String jsonStr = sh.makeServiceCall(url, ServiceHandler.POST);
Log.d("Response: ", "> " + jsonStr);
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
// Getting JSON Array node
sampleData = jsonObj.getJSONArray(TAG_STATUS);
// looping through All Contacts
for (int i = 0; i < sampleData.length(); i++) {
JSONObject c = sampleData.getJSONObject(i);
String name = c.getString(TAG_USERNAME);
String password = c.getString(TAG_PASSWORD);
// tmp hashmap for single contact
HashMap<String, String> contact = new HashMap<String, String>();
// adding each child node to HashMap key => value
contact.put(TAG_USERNAME, name);
contact.put(TAG_PASSWORD, password);
// adding contact to contact list
userNamePass.add(contact);
}
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Log.e("ServiceHandler", "Couldn't get any data from the url");
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// Dismiss the progress dialog
if (pDialog.isShowing())
pDialog.dismiss();
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(
MainActivity.this, userNamePass,
R.layout.list_item, new String[] { TAG_USERNAME, TAG_PASSWORD }, new int[] { R.id.name,
R.id.password });
setListAdapter(adapter);
}
}
}
ServiceHandler.Java
public class ServiceHandler {
static String response = null;
public final static int GET = 1;
public final static int POST = 2;
public ServiceHandler() {
}
/*
* Making service call
* @url - url to make request
* @method - http request method
* */
public String makeServiceCall(String url, int method) {
return this.makeServiceCall(url, method, null);
}
/*
* Making service call
* @url - url to make request
* @method - http request method
* @params - http request params
* */
public String makeServiceCall(String url, int method,
List<NameValuePair> params) {
try {
// http client
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpEntity httpEntity = null;
HttpResponse httpResponse = null;
// Checking http request method type
if (method == POST) {
HttpPost httpPost = new HttpPost(url);
// adding post params
if (params != null) {
httpPost.setEntity(new UrlEncodedFormEntity(params));
}
httpResponse = httpClient.execute(httpPost);
} else if (method == GET) {
// appending params to url
if (params != null) {
String paramString = URLEncodedUtils
.format(params, "utf-8");
url += "?" + paramString;
}
HttpGet httpGet = new HttpGet(url);
httpResponse = httpClient.execute(httpGet);
}
httpEntity = httpResponse.getEntity();
response = EntityUtils.toString(httpEntity);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return response;
}
}
SingleContactActivity.Java
public class SingleContactActivity extends Activity {
// JSON node keys
private static final String TAG_NAME = "name";
private static final String TAG_PASSWORD = "password";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_single_contact);
// getting intent data
Intent in = getIntent();
// Get JSON values from previous intent
String name = in.getStringExtra(TAG_NAME);
String password = in.getStringExtra(TAG_PASSWORD);
// Displaying all values on the screen
TextView lblName = (TextView) findViewById(R.id.name_label);
TextView lblPassword = (TextView) findViewById(R.id.password_label);
lblName.setText(name);
lblPassword.setText(password);
}
}
Error I am getting while running:
02-24 17:34:25.190: W/System.err(14285): org.apache.http.conn.ConnectTimeoutException: Connect to /123.xxx.x.xxx:443 timed out
02-24 17:34:25.190: W/System.err(14285): at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:121)
02-24 17:34:25.190: W/System.err(14285): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:156)
02-24 17:34:25.195: W/System.err(14285): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
02-24 17:34:25.195: W/System.err(14285): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
02-24 17:34:25.195: W/System.err(14285): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:359)
02-24 17:34:25.195: W/System.err(14285): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
02-24 17:34:25.200: W/System.err(14285): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
02-24 17:34:25.200: W/System.err(14285): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
02-24 17:34:25.200: W/System.err(14285): at com.oi.jsonparsingdemo.ServiceHandler.makeServiceCall(ServiceHandler.java:59)
02-24 17:34:25.205: W/System.err(14285): at com.oi.jsonparsingdemo.ServiceHandler.makeServiceCall(ServiceHandler.java:34)
02-24 17:34:25.210: W/System.err(14285): at com.oi.jsonparsingdemo.MainActivity$GetContacts.doInBackground(MainActivity.java:99)
02-24 17:34:25.210: W/System.err(14285): at com.oi.jsonparsingdemo.MainActivity$GetContacts.doInBackground(MainActivity.java:1)
02-24 17:34:25.215: W/System.err(14285): at android.os.AsyncTask$2.call(AsyncTask.java:185)
02-24 17:34:25.215: W/System.err(14285): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
02-24 17:34:25.215: W/System.err(14285): at java.util.concurrent.FutureTask.run(FutureTask.java:138)
02-24 17:34:25.220: W/System.err(14285): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
02-24 17:34:25.220: W/System.err(14285): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
02-24 17:34:25.220: W/System.err(14285): at java.lang.Thread.run(Thread.java:1019)
02-24 17:34:25.220: D/Response:(14285): > null
02-24 17:34:25.220: E/ServiceHandler(14285): Couldn't get any data from the url
02-24 17:34:25.280: D/CLIPBOARD(14285): Hide Clipboard dialog at Starting input: finished by someone else... !
Where i am wrong or what steps should i follow not to acheve my task. Please give your valuable suggestions...
Solution 2
Confirm that your URL is right or not. I faced the same issue as my URL is http:// and i am accessing it as https://.
May be your URL is like :
// URL to get contacts JSON
private static String url = "http://123.xxx.x.xxx/DEMO/login_data.php";
OTHER TIPS
Your URL is most likely invalid. Change it to your appropriate URL.
private static String url = "https://123.xxx.x.xxx/DEMO/login_data.php";
That just doesn't look like any URL I've ever seen...
In the event that you masked it for SO, then make sure you get something by typing the same URL into a web browser, including the formatted strings. The best way to ensure this is to print the URL used immediately before using it, to ensure that nothing unusual has been added, and that the URL works exactly as intended.
As this is a local URL, it could be that your device isn't connected to the appropriate network. Try pinging the local URL from your phone (There's several utilities to try this, just look around).