Jason درس 12 استاد - خواهشا اگه بلدین کمک کنید ، گیر اینم
دقیقا کد های درس 12 استاد ، واسه خوندن جسیون ، نمیدونم چرا کرش میشه
دقیقا همونه رو نوشتم ، لینک جیسون درسته ، با FireFox که میرم جیسون رو نشون میده
public class Webservice {
public static String readUrl(String url) {
try {
HttpClient client = new DefaultHttpClient();
HttpPost method = new HttpPost(url);
HttpResponse response = client.execute(method);
InputStream inputStream = response.getEntity().getContent();
String result = convertInputStreamToString(inputStream);
return result;
}
catch (ClientProtocolException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
return null;
}
private static String convertInputStreamToString(InputStream inputStream) {
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder builder = new StringBuilder();
String line = "";
while ((line = reader.readLine()) != null) {
builder.append(line);
}
return builder.toString();
}
catch (IOException e) {
e.printStackTrace();
}
return null;
}
---------------------------------------------------------------------
public class ActivityUpdate extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.update_layout);
// TODO Auto-generated method stub
String result = Webservice.readUrl("http://192.168.1.100/akaskhoone/");
if (result != null)
{
try {
JSONArray tasks = new JSONArray(result);
for (int i = 0; i < tasks.length(); i++) {
JSONObject task = tasks.getJSONObject(i);
Log.i("LOG", task.getString("name"));
}
}
catch (JSONException e) {
e.printStackTrace();
}
}
}
}
}
برای اتصال به سرور در ورژن های بالاتر اندروید باید از Thread استفاده کنید :
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
HttpClient client = new DefaultHttpClient();
HttpPost method = new HttpPost(url);
HttpResponse response = client.execute(method);
InputStream inputStream = response.getEntity().getContent();
String result = convertInputStreamToString(inputStream);
return result;
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
}
});
thread.start();
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().permitAll().build());
کافیه فقط همین کد رو بعد
try {
متود readUrl در کلاس WebSrvice قرار بدی و اجرا کنی پروژت رو
موفق باشی
من اینطوری نوشتم ببینید. منظور پاسخ بالا همین نبود
public class Webservice {
public static void readUrl(final String url, final ArrayList<NameValuePair> params) {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().permitAll().build());
HttpClient client = new DefaultHttpClient();
HttpPost method = new HttpPost(url);
if (params != null) {
// method.setEntity(new UrlEncodedFormEntity(params));
method.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
}
HttpResponse response = client.execute(method);
InputStream inputStream = response.getEntity().getContent();
String result = convertInputStreamToString(inputStream);
return result;// اینجا ارور دارم بازم
}
catch (ClientProtocolException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
catch (JSONException e) {
e.printStackTrace();
}
}
});
thread.start();
}
پاسخگویی و مشاهده پاسخ های این سوال تنها برای اعضای ویژه سایت امکان پذیر است .
چنانچه تمایل دارید به همه بخش ها دسترسی داشته باشید میتوانید از این بخش لایسنس این آموزش را خریداری نمایید .