我需要發送一些變量以存儲到數據庫中,並且一些要顯示在PHP
頁面的屏幕中。我沒有找到如何做到這一點,並進入網頁,以POST
方法顯示屏幕上的變量,所以我試圖通過URL
與GET
方法來通過它們。Android POST在POST中不起作用
POST
方法工作正常,變量存儲在數據庫中,但在此之後,應用程序不會轉到網頁並在屏幕上顯示另一個變量。這裏是我的代碼:
public class Main3Activity extends AppCompatActivity {
public String precio;
URL url;
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 void getMethod() throws IOException {
url = new URL("https://www.webpage.com/login_app.php?username=" + precio);
}
public class SendPostRequest extends AsyncTask<String, Void, String> {
protected void onPreExecute(){}
protected String doInBackground(String... arg0) {
try {
URL url = new URL("https://www.webpage.com/login_app.php"); // here is your URL path
JSONObject postDataParams = new JSONObject();
postDataParams.put("precio", "1€");
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("Success");
String line="";
getMethod();
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();
}
有人有想法解決它嗎?
謝謝你的時間。
編輯:
感謝@ user6749691,現在我已經得到了下面的腳本來執行GET方法:
private void openConnection(String method, URL url) {
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(15000 /* milliseconds */);
conn.setConnectTimeout(15000 /* milliseconds */);
conn.setRequestMethod(method);
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();
}
public URL getMethod() throws IOException {
return new URL("https://www.webpage.com/login_app.php?username=" + precio);
}
然後我調用該函數openConnection("GET", getMethod());
中的以下行'SendPostRequest' 類:
StringBuffer sb = new StringBuffer("Success");
String line="";
openConnection("GET", getMethod());
while((line = in.readLine()) != null) {
.
.
.
但我在獲得未處理的異常 'url.openConnection()' 的'conn.setRequestMethod(method)'中的d;'
謝謝你的回答。無法解析符號'Url' – alberzyzz
我編輯了我的文章,但請注意,這不是建議!如果您不知道自己在做什麼,這可能會導致很多麻煩,因此請嘗試在您的應用中添加改造或排除功能並使用它 – mayosk
感謝您的建議。這是一個僅用於測試和學習的應用程序。我將包括改造。無法解析符號postDataParams – alberzyzz