尝试从用户EditText
获取输入并将其格式化为URL $_GET
,以便将其保存到mysql数据库中。截至目前,该应用只会在每个EditText
字段中存储第一个值。
如果有人能给我一个关于我做错了什么的指示,那将是甜蜜的!
public class NewIdea extends Activity{
/** Called when the activity is first created. */
private List<EditText> editTextList = new ArrayList<EditText>();
private Button button;
private EditText idea, des;
private Object mPassword;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_screen);
button = (Button) findViewById(R.id.btnCreateIdea);
idea = (EditText) findViewById(R.id.inId);
des = (EditText) findViewById(R.id.inDis);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//StringBuilder params = new StringBuilder();
//for (int i = 0; i < des.length(); i++ ) {
//params.append(des.getText().toString());}
String mUsername = idea.getText().toString();
String mPassword = des.getText().toString();
String answer = null;
StringBuilder params = new StringBuilder(mPassword);
for(int i = 0; i < des.getText().length(); i++) {
params.append(des + "%" +i) ;
answer = params.toString();
}
tryLogin(mUsername, answer);
}
});
}
protected void tryLogin(String idea, String answer)
{
HttpURLConnection connection;
OutputStreamWriter request = null;
URL url = null;
String response = null;
String parameters = "?ideaname="+idea+"&description="+answer;
try
{
url = new URL("http://"server"/PHP/create_idea.php"+ parameters);
connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestMethod("GET");
request = new OutputStreamWriter(connection.getOutputStream());
request.write(parameters);
request.flush();
request.close();
String line = parameters;
InputStreamReader isr = new InputStreamReader(connection.getInputStream());
BufferedReader reader = new BufferedReader(isr);
StringBuilder sb = new StringBuilder();
while ((line = reader.readLine()) != null)
{
sb.append("%" + line);
}
// Response from server after login process will be stored in response variable.
response = sb.toString();
// You can perform UI operations here
Toast.makeText(this,"Message from Server: \n"+ response, 0).show();
isr.close();
reader.close();
}
catch(IOException e)
{
// Error
}
}
}
答案 0 :(得分:0)
使用用户名和密码时,永远不会理解使用GET请求,因为GET会将数据附加到公开显示的URL中。建议使用POST请求。您可以使用此方法将数据发布到服务器,只需将数据添加到NameaValuePair并添加为实体以发布请求。在服务器端,您可以使用
获取数据$_POST['key']
尝试这种方法
public static String HitServerPOST(String url,List<NameValuePair> nameValuePairs) {
DefaultHttpClient hc = new DefaultHttpClient();
ResponseHandler<String> res = new BasicResponseHandler();
HttpPost postMethod = new HttpPost(url);
String response = null;
try {
postMethod.setEntity(new UrlEncodedFormEntity(nameValuePairs));
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
response = hc.execute(postMethod, res);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return response;
}
并且呼叫将像
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("key1","value1"));
nameValuePairs.add(new BasicNameValuePair("key2","value2"));
String response = HitServerPOST(SERVER_URL,nameValuePairs);