navigation drawer fragment altında AsyncTask
merhabalar hocam. müsait olduğunuzda bakarsanız çok sevinirim.
navigation drawer fragment uygulaması yaptım. fragment in içinde şu kodları oluşturdum ama hemen hata verip çıkıyor.
public class WhatsHotFragment extends Fragment implements OnClickListener {
Button bSorgula;
EditText etID, etProductName, etCategoryName, etUnitPrice;
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState)
{
View view = inflater.inflate(
R.layout.fragment_whats_hot, container, false);
bSorgula = (Button) view.findViewById(R.id.bSorgula);
bSorgula.setOnClickListener(this);
etID = (EditText) view.findViewById(R.id.etID);
etProductName = (EditText) view.findViewById(R.id.etProductName);
etCategoryName = (EditText) view.findViewById(R.id.etCategoryName);
etUnitPrice = (EditText) view.findViewById(R.id.etUnitPrice);
return view;
}
@Override
public void onClick(View v) {
new myAsyncTask("Yükleniyor").execute();
}
private class myAsyncTask extends AsyncTask<Void, Void, Void> {
String modalMesaj;
ProgressDialog dialog;
JSONObject jsonObject = null;
String productName = "";
String categoryName = "";
String unitPrice = "";
public myAsyncTask(String mMesaj) {
this.modalMesaj = mMesaj;
// this.dialog = new ProgressDialog();
}
@Override
protected void onPreExecute() {
dialog.setMessage(modalMesaj);
dialog.setIndeterminate(true);
dialog.setCancelable(false);
dialog.show();
}
@Override
protected Void doInBackground(Void... params) {
Integer productID = Integer.valueOf(etID.getText().toString());
String url = "http://madenteknikerleri.net/calisma/Default.aspx?ProductId="
+ productID;
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet(url);
HttpResponse response;
try {
response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream instream = entity.getContent();
String result = convertStreamToString(instream);
jsonObject = new JSONObject(result);
instream.close();
}
} catch (ClientProtocolException e) {
Mesaj(e.getMessage());
} catch (IOException e) {
Mesaj(e.getMessage());
} catch (JSONException e) {
Mesaj(e.getMessage());
}
return null;
}
@Override
protected void onPostExecute(Void str) {
if (dialog.isShowing())
dialog.dismiss();
try {
productName = jsonObject.getString("ProductName").toString();
categoryName = jsonObject.getString("CategoryName").toString();
unitPrice = jsonObject.getString("UnitPrice").toString();
etProductName.setText(this.productName);
etCategoryName.setText(this.categoryName);
etUnitPrice.setText(this.unitPrice);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
private static String convertStreamToString(InputStream is) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
private void Mesaj(String s) {
Toast.makeText(this.getActivity(), s, Toast.LENGTH_LONG).show();
}
}