I m totally new to android & have a strange problem here. I m working on an application which receives data from the server running a WCF web-service.
Steps I m following :
- Calling the webservice.
- Web Service returns data in JSONArray format, thus i retrieve the HttpEntity and Response in String format.
- Convert the String to JSONObject and then to JSONArray to dislay in the list.
Everything is running perfect the data is retrieved, converted to JSONObject , then to JSONArray & then in listView using the BaseAdapter.
Now the problem is data gets loaded well in the emulater but when i transfer the apk file on my device/devices it starts throwing exception.
Strange fact, it runs well on the real device also till my JSONArray has like 3 to 4 objects on it. More than that if 5 to 6 objects, the app on the phone throws a java.net.socketexception recvfrom failed ebadf(Bad File Number) but the same with many objects runs flawless on the emulator *
Any Help would be great, m on a deadline and i m trying my best to learn and solve it.
Here is the class where i call the server and retrieve the string data.
public class ConnectToDB
{
private static final String SVC_URL = "http://ift.tt/1ImcWrV";
int statusCode = 400;
JSONArray jsonArray;
String result;
@TargetApi(Build.VERSION_CODES.KITKAT)
public String getStories()
{
HttpGet request = new HttpGet(SVC_URL +"liststory");
request.setHeader("Accept", "application/json");
request.setHeader("Content-type", "application/json");
try
{
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpResponse response = httpClient.execute(request);
HttpEntity responseEntity = response.getEntity();
response = httpClient.execute(request);
result="1";
InputStream stream = responseEntity.getContent();
result="2";
BufferedReader reader = new BufferedReader(
new InputStreamReader(stream));
result="3";
StringBuilder builder = new StringBuilder();
String line;
result="4";
while ((line = reader.readLine()) != null)
{
builder.append(line);
}
stream.close();
result="5"; // Error Occurs here, cause the error log,doesn't reach 5, it prints till 4 only
result = builder.toString();
} catch (Exception e)
{
result="Error :"+e+"at "+result;
}
return result;
}
}
here is the fragment which has the listview, its BaseAdapter & the AysncTask Call to call to server, here i retrieve the data from the server in String format, convert it to JSONObject to JSONArray and pass it to the adapter
public class Entertainment extends Fragment {
private ListView listView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.cat_entertainment_fragment, container, false);
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
listView = (ListView) getActivity().findViewById(R.id.entertainmentList);
new getStoryData().execute(new ConnectToDB());
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
});
}
public class EntertainmentListAdapter extends BaseAdapter {
JSONArray entertainmentListArray;
private Activity activity;
private LayoutInflater layoutInflater;
private EntertainmentListAdapter(JSONArray jsonArray, Activity activity) {
this.entertainmentListArray = jsonArray;
this.activity = activity;
layoutInflater = (LayoutInflater) this.activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return this.entertainmentListArray.length();
}
@Override
public Object getItem(int i) {
return i;
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
ListCell cell;
if (view == null) {
view = layoutInflater.inflate(R.layout.row_published_stories, null);
cell = new ListCell();
cell.storyID = ((TextView) view.findViewById(R.id.storyID));
cell.storyHeader = ((TextView) view.findViewById(R.id.post_header));
cell.storyContent = ((TextView) view.findViewById(R.id.post_description));
cell.likes = ((TextView) view.findViewById(R.id.storyLikes));
cell.dislikes = ((TextView) view.findViewById(R.id.storyDislike));
cell.byline = ((TextView) view.findViewById(R.id.storyBy));
cell.storyDate = ((TextView) view.findViewById(R.id.storyPublishedDate));
cell.imageString = ((TextView) view.findViewById(R.id.storyImage));
view.setTag(cell);
} else {
cell = (ListCell) view.getTag();
}
try {
JSONObject jsonObject = this.entertainmentListArray.getJSONObject(i);
cell.storyID.setText(jsonObject.getString("Id"));
cell.storyHeader.setText(jsonObject.getString("StoryHeader"));
cell.storyContent.setText(jsonObject.getString("StoryContent"));
cell.likes.setText(jsonObject.getString("Likes"));
cell.dislikes.setText(jsonObject.getString("Dislikes"));
cell.byline.setText(jsonObject.getString("ByLine"));
cell.storyDate.setText(jsonObject.getString("StoryDate"));
cell.imageString.setText(jsonObject.getString("ImageUrl"));
} catch (JSONException e) {
e.printStackTrace();
}
return view;
}
}
void setAdapter(JSONArray array) {
listView.setAdapter(new EntertainmentListAdapter(array, getActivity()));
}
private class ListCell {
TextView likes, dislikes, storyHeader, storyContent, byline, storyID, storyDate, imageString;
CircularImageView storyImage;
}
class getStoryData extends AsyncTask<ConnectToDB, Void, String> {
private final ProgressDialog dialog = new ProgressDialog(getActivity());
@Override
protected String doInBackground(ConnectToDB... params) {
return params[0].getStories();
}
@Override
protected void onPreExecute() {
this.dialog.setMessage("Loading data");
this.dialog.setCancelable(false);
this.dialog.show();
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
try {
if (result.startsWith("E")) {
Toast.makeText(getActivity(), "Check Your Internet Connection.", Toast.LENGTH_LONG).show();
this.dialog.dismiss();
} else {
this.dialog.dismiss();
JSONObject jObject = new JSONObject(result);
JSONArray jArray = jObject.getJSONArray("ListStoryResult");
setAdapter(jArray);
Toast.makeText(getActivity(), jArray.length() + " Stories Loaded.", Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
Toast.makeText(getActivity(), "Error :" + e, Toast.LENGTH_LONG).show();
}
}
}
}
Aucun commentaire:
Enregistrer un commentaire