致力于从用户那里获取输入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
}
}
}