我想用genymotion模拟器制作HTTPRequest。 (没有物理设备......)。 但我收到一个错误:
W/EGL_genymotion﹕ eglSurfaceAttrib not implemented
D/OpenGLRenderer﹕ Enabling debug mode 0
D/dalvikvm﹕ GC_CONCURRENT freed 262K, 16% free 4387K/5216K, paused 1ms+2ms, total 12ms
W/System.err﹕ org.json.JSONException: No value for success
并且HTTP请求失败。
连接到服务器并调用请求:
CallServer getGender = new CallServer();
getGender.execute();
这是我的CallServer类:
public class CallServer extends AsyncTask<String, String, String> {
int success = 0;
String urlAddress;
String data;
public CallServer()
{
data = null;
urlAddress = "https://taub-prayer-ramym.c9.io/checkMode";
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... args) {
try {
final String TAG_SUCCESS = "success";
JSONParser jsonParser = new JSONParser();
Log.i("LetsRun", "sending message to server...");
// getting product details by making HTTP request
JSONObject json = jsonParser.makeHttpRequest(
urlAddress, "GET",null);
if(json == null)
{
return "Can't connect to server.";
}
data = json.getString(TAG_SUCCESS);
if (data == null){
return "ERROR!";
}
else{ // on success
System.out.print(data);
return data;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String file_url) {
super.onPostExecute(file_url);
}
public String getData() {
return data;
}
}
这是我的JSONParser类:
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
// constructor
public JSONParser() {
}
public JSONObject getJSONFromUrl(final String url) {
// Making HTTP request
try {
// Construct the client and the HTTP request.
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
// Execute the POST request and store the response locally.
HttpResponse httpResponse = httpClient.execute(httpPost);
// Extract data from the response.
HttpEntity httpEntity = httpResponse.getEntity();
// Open an inputStream with the data content.
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
// Create a BufferedReader to parse through the inputStream.
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
// Declare a string builder to help with the parsing.
StringBuilder sb = new StringBuilder();
// Declare a string to store the JSON object data in string form.
String line = null;
// Build the string until null.
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
// Close the input stream.
is.close();
// Convert the string builder data to an actual string.
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// Try to 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 the JSON Object.
return jObj;
}
// 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();
} 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;
}
}
我已尝试过Chrome上的服务器请求并且运行良好...有人可以在硬件设备上尝试吗?什么是错误的任何建议?非常感谢
编辑: 完整错误:
D/dalvikvm﹕ Late-enabling CheckJNI
08-26 06:40:17.424 1333-1339/com.example.atheel.prayer D/dalvikvm﹕ Debugger has detached; object registry had 1 entries
08-26 06:40:17.512 1333-1333/com.example.atheel.prayer I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.internal.widget.TintTypedArray.getChangingConfigurations
08-26 06:40:17.512 1333-1333/com.example.atheel.prayer W/dalvikvm﹕ VFY: unable to resolve virtual method 608: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
08-26 06:40:17.532 1333-1333/com.example.atheel.prayer D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
08-26 06:40:17.556 1333-1333/com.example.atheel.prayer I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.internal.widget.TintTypedArray.getType
08-26 06:40:17.556 1333-1333/com.example.atheel.prayer W/dalvikvm﹕ VFY: unable to resolve virtual method 630: Landroid/content/res/TypedArray;.getType (I)I
08-26 06:40:17.560 1333-1333/com.example.atheel.prayer D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
08-26 06:40:17.592 1333-1345/com.example.atheel.prayer I/LetsRun﹕ sending message to server...
08-26 06:40:17.656 1333-1336/com.example.atheel.prayer D/dalvikvm﹕ GC_CONCURRENT freed 196K, 16% free 4254K/5016K, paused 3ms+1ms, total 13ms
08-26 06:40:17.700 1333-1333/com.example.atheel.prayer D/libEGL﹕ loaded /system/lib/egl/libEGL_genymotion.so
08-26 06:40:17.704 1333-1333/com.example.atheel.prayer D/﹕ HostConnection::get() New Host Connection established 0xb8512dd8, tid 1333
08-26 06:40:17.712 1333-1333/com.example.atheel.prayer D/libEGL﹕ loaded /system/lib/egl/libGLESv1_CM_genymotion.so
08-26 06:40:17.712 1333-1333/com.example.atheel.prayer D/libEGL﹕ loaded /system/lib/egl/libGLESv2_genymotion.so
08-26 06:40:17.792 1333-1333/com.example.atheel.prayer W/EGL_genymotion﹕ eglSurfaceAttrib not implemented
08-26 06:40:17.804 1333-1333/com.example.atheel.prayer D/OpenGLRenderer﹕ Enabling debug mode 0
08-26 06:40:17.868 1333-1336/com.example.atheel.prayer D/dalvikvm﹕ GC_CONCURRENT freed 269K, 17% free 4372K/5208K, paused 2ms+0ms, total 8ms
08-26 06:40:18.208 1333-1345/com.example.atheel.prayer W/System.err﹕ org.json.JSONException: No value for success
08-26 06:40:18.208 1333-1345/com.example.atheel.prayer W/System.err﹕ at org.json.JSONObject.get(JSONObject.java:354)
08-26 06:40:18.208 1333-1345/com.example.atheel.prayer W/System.err﹕ at org.json.JSONObject.getString(JSONObject.java:510)
08-26 06:40:18.208 1333-1345/com.example.atheel.prayer W/System.err﹕ at com.example.atheel.prayer.CallServer.doInBackground(CallServer.java:79)
08-26 06:40:18.212 1333-1345/com.example.atheel.prayer W/System.err﹕ at com.example.atheel.prayer.CallServer.doInBackground(CallServer.java:17)
08-26 06:40:18.212 1333-1345/com.example.atheel.prayer W/System.err﹕ at android.os.AsyncTask$2.call(AsyncTask.java:287)
08-26 06:40:18.212 1333-1345/com.example.atheel.prayer W/System.err﹕ at java.util.concurrent.FutureTask.run(FutureTask.java:234)
08-26 06:40:18.212 1333-1345/com.example.atheel.prayer W/System.err﹕ at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
08-26 06:40:18.212 1333-1345/com.example.atheel.prayer W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
08-26 06:40:18.212 1333-1345/com.example.atheel.prayer W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
08-26 06:40:18.212 1333-1345/com.example.atheel.prayer W/System.err﹕ at java.lang.Thread.run(Thread.java:856)
08-26 06:40:48.736 1333-1336/com.example.atheel.prayer D/dalvikvm﹕ GC_CONCURRENT freed 356K, 18% free 4400K/5324K, paused 2ms+1ms, total 4ms