بروز خطا
[message]
اشتراک در سوال
رای ها
[dataList]
مشکل اتصال به localhost و دریافت داده های json
نمیتونم امولاتور رو به localhost وصل کنم
package ir.xzn.tarahi.test_json_downloader;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONArray;
import org.json.JSONObject;
public class MainActivity extends AppCompatActivity {
private RequestQueue myRequsetQueue;
private TextView txt;
public final String log_label = "abtin message";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt = (TextView) findViewById(R.id.txt);
}
public void onBtnGetDataClicked(View v) {
myRequsetQueue = Volley.newRequestQueue(this);
JsonObjectRequest jObj = new JsonObjectRequest(Request.Method.GET,
"http://localhost:81/abtin/get_cat.php",
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray array = response.getJSONArray("cat");
for (int i = 0; i < array.length(); i++) {
JSONObject temp = array.getJSONObject(i);
String str = temp.getString("id") + " " + temp.getString("name") + " [" +
temp.getString("amount") + "]";
Log.i(log_label, "error in JSON_downloder() -> " + str);
txt.append(str + "\n");
}
} catch (Exception e) {
txt.setText("error in download");
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.i(log_label, "error in JSON_downloder() -> " + error.getMessage());
txt.setText("error" + "\n");
}
}
);
myRequsetQueue.add(jObj);
}
}
+1
0
کاش کدتون رو تمیز میکردید. نباید بنویسید localhost باید جاش local ip خودتون رو بذارید. یک چیزی شبیه 192.168.1.10. اینجوری نه تنها امولاتور توی گوشیای که به وایفایتون متصل باشه هم کار میکنه. (10 سال پیش)
برای این سوال پاسخی وجود ندارد.
پاسخگویی و مشاهده پاسخ های این سوال تنها برای اعضای ویژه سایت امکان پذیر است .
چنانچه تمایل دارید به همه بخش ها دسترسی داشته باشید میتوانید از این بخش لایسنس این آموزش را خریداری نمایید .