我正在尝试使用httppost连接到服务器,但我不能因为某些错误。 这是我的android代码:
public class SendPostRequest extends AsyncTask<String, Void, String> {
protected void onPreExecute(){}
protected String doInBackground(String... arg0) {
try{
URL url = new URL("http://abhirathod.byethost4.com/post.php");
JSONObject postDataParams = new JSONObject();
postDataParams.put("name", "abc");
postDataParams.put("email", "abc@gmail.com");
Log.e("params", postDataParams.toString());
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(15000 /* milliseconds */);
conn.setConnectTimeout(15000 /* milliseconds */);
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os, "UTF-8"));
writer.write(getPostDataString(postDataParams));
int responseCode=conn.getResponseCode();
if (responseCode == HttpsURLConnection.HTTP_OK) {
BufferedReader in=new BufferedReader(
new InputStreamReader(
conn.getInputStream()));
StringBuffer sb = new StringBuffer("");
String line="";
while((line = in.readLine()) != null) {
sb.append(line);
break;
}
in.close();
return sb.toString();
}
else {
return new String("false : "+responseCode);
}
}
catch(Exception e){
return new String("Exception: " + e.getMessage());
}
}
@Override
protected void onPostExecute(String result) {
Toast.makeText(getApplicationContext(), result,
Toast.LENGTH_LONG).show();
}
}
public String getPostDataString(JSONObject params) throws Exception {
StringBuilder result = new StringBuilder();
boolean first = true;
Iterator<String> itr = params.keys();
while(itr.hasNext()){
String key= itr.next();
Object value = params.get(key);
if (first)
first = false;
else
result.append("&");
result.append(URLEncoder.encode(key, "UTF-8"));
result.append("=");
result.append(URLEncoder.encode(value.toString(), "UTF-8"));
}
return result.toString();
}
我一次调用execute方法。我得到了这个输出:
https://s23.postimg.org/47dwhsxvf/Screenshot_2016_12_24_11_05_49_com_spellbee_admi.png
Plsz帮忙!
答案 0 :(得分:-2)
final RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext()); final StringRequest stringRequest = new StringRequest(Request.Method.POST,Appconfig.Url,new Response.Listener(){ @覆盖 public void onResponse(String response){
//compile 'com.mcxiaoke.volley:library:1.0.19'
// <uses-permission android:name="android.permission.INTERNET"/>
try {
jsonObject = new JSONObject(response);
JSONArray jsonArray = jsonObject.getJSONArray("Bros");
for(int i=0; i<jsonArray.length(); i++) {
JSONObject jsonObject1 = jsonArray.getJSONObject(i);
String SONG_ID = jsonObject1.getString("SONG_ID");
String GroupID = jsonObject1.getString("SONG_NAME");
String SONG_PIC = jsonObject1.getString("SONG_PIC");
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(),error.toString(),Toast.LENGTH_LONG).show();
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> parms = new HashMap<>();
// parms.put("MessageID",Model.getMESSAGE_ID());
return parms;
}
};
requestQueue.add(stringRequest);
}