2014-10-11 71 views
0

我想用一些参数向服务器发送一个json请求。请求会和异步任务是工作的罚款,但它在服务器抛出异常,说无效的网址使用异步任务发送带有参数的json请求android

下面是我在做什么

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity); 

    Button btnChart = (Button) findViewById(R.id.btn_chart); 

    // Defining click event listener for the button btn_chart 
    OnClickListener clickListener = new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      new HttpAsyncTask().execute("https://tt.student.com/back.json"); 
       } 
    }; 


    // Setting event click listener for the button btn_chart of the MainActivity layout 
    btnChart.setOnClickListener(clickListener); 
} 

public static String POST(String url){ 
    InputStream inputStream = null; 
    String result = ""; 
    try { 

     // 1. create HttpClient 
     HttpClient httpclient = getNewHttpClient(); 

     // 2. make POST request to the given URL 
     HttpPost httpPost = new HttpPost(url); 

     String json = ""; 

     // 3. build jsonObject 
     JSONObject jsonObject = new JSONObject(); 
     jsonObject.accumulate("user", 1); 
     jsonObject.accumulate("student_id", 1); 
     jsonObject.accumulate("user_email", "[email protected]"); 
     jsonObject.accumulate("from", "Fri Oct 10 12:38:00 2014 GMT+0200"); 
     jsonObject.accumulate("to", "Sat Oct 11 12:38:00 2014 GMT+0200"); 

     // 4. convert JSONObject to JSON to String 
     json = jsonObject.toString(); 

     // ** Alternative way to convert Person object to JSON string usin Jackson Lib 
     // ObjectMapper mapper = new ObjectMapper(); 
     // json = mapper.writeValueAsString(person); 

     // 5. set json to StringEntity 
     StringEntity se = new StringEntity(json); 

     // 6. set httpPost Entity 
     httpPost.setEntity(se); 

     // 7. Set some headers to inform server about the type of the content 
     httpPost.setHeader("Accept", "application/json"); 
     httpPost.setHeader("Content-type", "application/json"); 

     // 8. Execute POST request to the given URL 
     HttpResponse httpResponse = httpclient.execute(httpPost); 

     // 9. receive response as inputStream 
     inputStream = httpResponse.getEntity().getContent(); 

     // 10. convert inputstream to string 
     if(inputStream != null) 
      result = convertInputStreamToString(inputStream); 
     else 
      result = "Did not work!"; 

    } catch (Exception e) { 
     Log.d("InputStream", e.getLocalizedMessage()); 
    } 

    // 11. return result 
    return result; 
} 
private static String convertInputStreamToString(InputStream inputStream) throws IOException{ 
    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); 
    String line = ""; 
    String result = ""; 
    while((line = bufferedReader.readLine()) != null) 
     result += line; 

    inputStream.close(); 
    return result; 

} 
private class HttpAsyncTask extends AsyncTask<String, Void, String> { 
    @Override 
    protected String doInBackground(String... urls) { 

     return POST(urls[0]); 
    } 
    // onPostExecute displays the results of the AsyncTask. 
    @Override 
    protected void onPostExecute(String result) { 
     Toast.makeText(getBaseContext(), "Received!", Toast.LENGTH_LONG).show(); 
     Log.d(TAG,result); 
    } 
} 

在URL中我都尝试的方法之一是在上面的代码和另一种是在url本身传递参数https://tt.student.com/back.json?user=1&student_id=1&[email protected]&from=Fri Oct 10 12:38:00 2014 GMT+0200&to=Sat Oct 11 12:38:00 2014 GMT+0200 为此它说URL中的非法字符...

+0

而不是GET请求你应该使用POST请求,电子邮件ID将被视为无效的查询字符串。 – 2014-10-11 11:25:33

+0

GET只是方法,但里面我正在做POST请求,为什么电子邮件将无效? – user3290805 2014-10-11 11:26:57

+0

添加服务器端代码也 – 2014-10-11 11:31:56

回答

1

我认为,问题可能与服务器或Wifi Supplicate状态。你的设备只连接到wifi,但没有通过真正的访问互联网或更专业,从服务器交换数据包。

我建议你使用方法来检查设备的连接性,我认为你是这种情况,因为我有类似的状态,我花了将近1-2个小时才能使它工作。 Internet连接检查这里是链接

Internet COnnection

我希望这会有所帮助。

+0

是的,情况是这样的,这是迟了一点,但我接受它,因为它是正确的答案。不管怎么说,多谢拉 – user3290805 2014-11-11 21:17:31

0

链接https://tt.student.com/back.json需要一个应该被android信任的证书。可能您可以通过代码接受证书,但您需要该证书。我试图打开浏览器,它显示我不受信任的证书....

+0

我已经添加了方法来获得证书只是没有添加在上面的代码,此外这是不正确的网址只是例子 – user3290805 2014-10-11 11:28:00

+0

然后你没有提出正确的请求...问谁做了服务器在哪个方式,他需要的请求..即他需要在头和什么应该是身体....你可以看到请求实体,你在服务器上发布... – karan421 2014-10-11 11:33:00

+0

但当我通过httpsclientin chrome发送请求https://tt.student.com/back.json?user=1&student_id=1&[email protected]&from=Fri Oct 10 12:38:00 2014 GMT + 0200&to = Sat 10月11日12 : 38:00 2014 GMT + 0200 请求,我得到正确的回复 – user3290805 2014-10-11 11:35:05