我已閱讀了許多帖子,並且嘗試應用其解決方案,但沒有人爲我工作。我對Java和Android相當陌生。網址是「https」,這不是錯誤。我的java代碼如下:響應代碼500 Android POST方法
public class Main3Activity extends AppCompatActivity {
Button b1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
b1 = (Button) findViewById(R.id.button_pago);
b1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
try {
new SendPostRequest().execute();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
public class SendPostRequest extends AsyncTask<String, Void, String> {
protected void onPreExecute(){}
protected String doInBackground(String... arg0) {
try {
URL url = new URL("https://www.myweb.com/login_app.php");
JSONObject postDataParams = new JSONObject();
postDataParams.put("precio", "5000");
postDataParams.put("texto", "f...h");
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));
writer.flush();
writer.close();
os.close();
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();
}
}
非常感謝你的時間。
檢查日誌文件中,我發現很多信息類似如下:
81.184.8.77 - - [23/Aug/2016:12:54:05 +0200] "POST /login_app.php HTTP/1.1" 500 379 "-" "Dalvik/1.6.0 (Linux; U; Android 4.4.2; Android SDK built for x86 Build/KK)" "-"
81.184.8.77 - - [23/Aug/2016:12:59:37 +0200] "POST /login_app.php HTTP/1.1" 500 10793 "-" "Dalvik/2.1.0 (Linux; U; Android 5.1.1; ONE E1001 Build/LMY47V)" "-"
81.184.8.77 - - [23/Aug/2016:13:01:05 +0200] "POST /login_app.php HTTP/1.1" 500 10807 "-" "Dalvik/1.6.0 (Linux; U; Android 4.4.2; Android SDK built for x86 Build/KK)" "-"
81.184.8.77 - - [23/Aug/2016:13:01:07 +0200] "POST /login_app.php HTTP/1.1" 500 379 "-" "Dalvik/1.6.0 (Linux; U; Android 4.4.2; Android SDK built for x86 Build/KK)" "-"
500內部服務器錯誤。因此請查看服務器日誌文件以瞭解發生了什麼。 – Jens
謝謝你的回答。我更新了原始帖子 – alberzyzz
這是Access日誌,在那裏你看不到任何錯誤。 – Jens