• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

client server connectivity problem

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a android activity which has to search for hotels of the particular city id passed to local server through php page trying to connect with mysql database and send response back to activity , but my application is not able to establish connection with php script, here is my source code and logcat errors , please help me out , i m struck in this problem for past one week , if anybody could point out the problem in code or give an alternate solution:-

logcat error

07-06 06:53:25.564: E/Buffer Error(1118): Error converting result java.io.IOException: Attempted read on closed stream.
07-06 06:53:25.784: E/Buffer Error(1118): Error converting result java.io.IOException: Attempted read on closed stream.
07-06 06:53:26.034: E/Buffer Error(1118): Error converting result java.io.IOException: Attempted read on closed stream.
07-06 06:53:29.398: E/Buffer Error(1118): Error converting result java.io.IOException: Attempted read on closed stream.
07-06 06:53:31.316: E/Buffer Error(1118): Error converting result java.io.IOException: Attempted read on closed stream.
07-06 06:54:34.553: E/Buffer Error(1118): Error converting result java.io.IOException: Attempted read on closed stream.
07-06 06:54:35.413: E/Buffer Error(1118): Error converting result java.io.IOException: Attempted read on closed stream.
07-06 06:54:38.023: E/Buffer Error(1118): Error converting result java.io.IOException: Attempted read on closed stream.
07-06 06:54:43.703: E/Buffer Error(1118): Error converting result java.io.IOException: Attempted read on closed stream.
07-06 06:54:45.143: E/Buffer Error(1118): Error converting result java.io.IOException: Attempted read on closed stream.
07-06 06:54:47.887: E/Buffer Error(1118): Error converting result java.io.IOException: Attempted read on closed stream.
07-06 06:54:48.106: E/Buffer Error(1118): Error converting result java.io.IOException: Attempted read on closed stream.
07-06 06:55:09.133: E/Buffer Error(1118): Error converting result java.io.IOException: Attempted read on closed stream.
07-06 06:55:12.453: E/Buffer Error(1118): Error converting result java.io.IOException: Attempted read on closed stream.
07-06 06:55:15.673: E/Buffer Error(1118): Error converting result java.io.IOException: Attempted read on closed stream.
07-06 06:55:15.863: E/Buffer Error(1118): Error converting result java.io.IOException: Attempted read on closed stream.
07-06 06:55:15.996: E/Buffer Error(1118): Error converting result java.io.IOException: Attempted read on closed stream.
07-06 06:55:16.283: E/Buffer Error(1118): Error converting result java.io.IOException: Attempted read on closed stream.
07-06 06:55:16.803: E/Buffer Error(1118): Error converting result java.io.IOException: Attempted read on closed stream.
07-06 06:55:59.799: E/Buffer Error(1118): Error converting result java.io.IOException: Attempted read on closed stream.
07-06 06:56:00.984: E/Buffer Error(1118): Error converting result java.io.IOException: Attempted read on closed stream.
07-06 06:56:01.893: E/Buffer Error(1118): Error converting result java.io.IOException: Attempted read on closed stream.
07-06 06:56:01.993: E/Buffer Error(1118): Error converting result java.io.IOException: Attempted read on closed stream.
07-06 06:56:02.527: E/Buffer Error(1118): Error converting result java.io.IOException: Attempted read on closed stream.
07-06 06:58:51.373: E/Buffer Error(1197): Error converting result java.lang.NullPointerException: lock == null
07-06 06:58:51.373: E/JSON Parser(1197): Error parsing data org.json.JSONException: End of input at character 0 of
07-06 06:58:51.453: E/AndroidRuntime(1197): FATAL EXCEPTION: AsyncTask #1
07-06 06:58:51.453: E/AndroidRuntime(1197): java.lang.RuntimeException: An error occured while executing doInBackground()
07-06 06:58:51.453: E/AndroidRuntime(1197): at android.os.AsyncTask$3.done(AsyncTask.java:299)
07-06 06:58:51.453: E/AndroidRuntime(1197): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
07-06 06:58:51.453: E/AndroidRuntime(1197): at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
07-06 06:58:51.453: E/AndroidRuntime(1197): at java.util.concurrent.FutureTask.run(FutureTask.java:239)
07-06 06:58:51.453: E/AndroidRuntime(1197): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
07-06 06:58:51.453: E/AndroidRuntime(1197): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
07-06 06:58:51.453: E/AndroidRuntime(1197): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
07-06 06:58:51.453: E/AndroidRuntime(1197): at java.lang.Thread.run(Thread.java:856)
07-06 06:58:51.453: E/AndroidRuntime(1197): Caused by: java.lang.NullPointerException
07-06 06:58:51.453: E/AndroidRuntime(1197): at com.example.go2kashmir.HotelbookActivity$GetHotel.doInBackground(HotelbookActivity.java:152)
07-06 06:58:51.453: E/AndroidRuntime(1197): at com.example.go2kashmir.HotelbookActivity$GetHotel.doInBackground(HotelbookActivity.java:1)
07-06 06:58:51.453: E/AndroidRuntime(1197): at android.os.AsyncTask$2.call(AsyncTask.java:287)
07-06 06:58:51.453: E/AndroidRuntime(1197): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
07-06 06:58:51.453: E/AndroidRuntime(1197): ... 4 more
07-06 06:58:54.303: E/Buffer Error(1197): Error converting result java.lang.NullPointerException: lock == null
07-06 06:58:54.383: E/JSON Parser(1197): Error parsing data org.json.JSONException: End of input at character 0 of

source code

public class HotelbookActivity extends Activity implements View.OnClickListener {
private String resp;

private ProgressDialog pDialog;

// Creating JSON Parser object
JSONParser jParser = new JSONParser();

ArrayList<HashMap<String, String>> hotelsList;
HashMap<String, String> map;
// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_HOTEL = "hotels";
private static final String TAG_PID = "hotel_id";
private static final String TAG_NAME = "hotel_name";
private static final String TAG_AREA = "area";
private static final String TAG_RATING = "star_rating";
private static final String TAG_IMAGE = "image";
private static final String TAG_GOVRATE = "govrating";
private static final String TAG_PRICE = "price";

private String city_id = "feedbacktype3";
// products JSONArray
JSONArray hotels = null;

// Button button;
Button sendPostReqButton;

private static final String url = "some url ****";

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

// Hashmap for ListView
hotelsList = new ArrayList<HashMap<String, String>>();

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
sendPostReqButton = (Button) findViewById(R.id.button1);
sendPostReqButton.setOnClickListener(this);

}

@Override
public void onClick(View v) {
// Get the data
// Loading Hotels in Background Thread
new GetHotel().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.activity_hotelbook, menu);
return true;
}

class GetHotel extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
/*
* pDialog = new ProgressDialog(HotelbookActivity.this);
* pDialog.setMessage("Loading hotels. Please wait...");
* pDialog.setIndeterminate(false); pDialog.setCancelable(false);
* pDialog.show();
*/
}

protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("cityid", city_id));

// getting JSON string from URL
JSONObject json = jParser.makeHttpRequest(url, "GET", params);

// Check your log cat for JSON reponse
Log.d("All hotels: ", json.toString());

try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);

if (success == 1) {
// products found
// Getting Array of Products
hotels = json.getJSONArray(TAG_HOTEL);

// looping through All Products
for (int i = 0; i < hotels.length(); i++) {
JSONObject c = hotels.getJSONObject(i);

// Storing each json item in variable
String id = c.getString(TAG_PID);
String name = c.getString(TAG_NAME);
String area = c.getString(TAG_AREA);
String rating = c.getString(TAG_RATING);
String image = c.getString(TAG_IMAGE);
String price = c.getString(TAG_PRICE);
String govrate = c.getString(TAG_GOVRATE);
// creating new HashMap
map = new HashMap<String, String>();

// adding each child node to HashMap key => value
map.put(TAG_PID, id);
map.put(TAG_NAME, name);
map.put(TAG_AREA, area);
map.put(TAG_RATING, rating);
map.put(TAG_IMAGE, image);
map.put(TAG_PRICE, price);
map.put(TAG_GOVRATE, govrate);

// adding HashList to ArrayList
hotelsList.add(map);
}

}
} catch (JSONException e) {
Log.e("response error", json.toString(), e);
}

return null;
}

protected void onPostExecute(String file_url) {

Intent intent = new Intent(HotelbookActivity.this,
HotelSearch.class);
intent.putExtra("map", map);

}
}

}


JSONParser class
public class JSONParser {

static InputStream is = null;
static JSONObject jObj = null;
static String json = "";

// constructor
public JSONParser() {

}

// function get json from url
// by making HTTP POST or GET mehtod
public JSONObject makeHttpRequest(String url, String method,
List<NameValuePair> params) {

// Making HTTP request
try {

// check for request method
if (method == "POST") {
// request method is POST
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params));

HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();

} else if (method == "GET") {
// request method is GET
DefaultHttpClient httpClient = new DefaultHttpClient();
String paramString = URLEncodedUtils.format(params, "utf-8");
url += "?" + paramString;
HttpGet httpGet = new HttpGet(url);

HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}

} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;

while ((line = reader.readLine()) != null) {
sb.append(line + "\n");

is.close();
json = sb.toString();
json = json.replaceAll("db_connect.php", "");
}
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}

// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}

// return JSON String
return jObj;

}
}
I have tested php script on local server and it is running fine. I am a newbie to android and this forum if this is not the right place to ask this question kindly redirect me to best place.

Edit/Delete Message
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not sure what you have been debugging for an entire week, but this NPE looks like it might be the cause:


07-06 06:58:51.453: E/AndroidRuntime(1197): Caused by: java.lang.NullPointerException
07-06 06:58:51.453: E/AndroidRuntime(1197): at com.example.go2kashmir.HotelbookActivity$GetHotel.doInBackground(HotelbookActivity.java:152)
07-06 06:58:51.453: E/AndroidRuntime(1197): at com.example.go2kashmir.HotelbookActivity$GetHotel.doInBackground(HotelbookActivity.java:1)


Find out what object is null, and whether it's OK to be null. Depending on that, either handle it being null, or fix it so it isn't null.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic