在我的应用程序中,我在我的应用程序中使用http服务,我需要一个编辑文本和两个按钮(预览按钮,发送按钮)。当我点击预览按钮时,http请求将与编辑文本的内容建立。我希望在对话框中显示响应和编辑文本内容。
这是网址: " http://invitations.in/events/register/credit.php?template=&#34 + sms_content
以下是回复:
{"学分":2}
这是我的代码:
editText = (EditText)findViewById(R.id.sms_content);
preview_btn = (Button) findViewById(R.id.preview_btn);
send_btn = (Button) findViewById(R.id.send_btn);
send_btn.setVisibility(View.GONE);
preview_btn.setVisibility(View.VISIBLE);
preview_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
sms_content = editText.getText().toString();
// get prompts.xml view
LayoutInflater li = LayoutInflater.from(context);
View promptsView = li.inflate(R.layout.prompts, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
context);
// set prompts.xml to alertdialog builder
alertDialogBuilder.setView(promptsView);
TextView preview = (TextView) promptsView
.findViewById(R.id.preview);
TextView smscount = (TextView) promptsView.findViewById(R.id.count_credits);
TextView numofcont = (TextView) promptsView.findViewById(R.id.no_ofcont);
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
try {
sms_content = URLEncoder.encode(sms_content, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
try {
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpPost = new HttpGet("http://v.leonas.in/events/register/credit.php?template="+sms_content);
HttpResponse response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
String responseString = EntityUtils.toString(entity);
JSONObject jsonObject = new JSONObject(responseString);
creditspersms = jsonObject.getInt("credits");
System.out.println(creditspersms);
} catch (JSONException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}catch (Exception e) {
e.printStackTrace();
}
}
});
thread.start();
preview.setText(sms_content);
smscount.setText(String.valueOf(creditspersms * contnum));
numofcont.setText(Integer.toString(contnum));
// set dialog message
alertDialogBuilder
.setCancelable(false)
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// get user input and set it to result
// edit text
editText.setText(editText.getText());
send_btn.setVisibility(View.VISIBLE);
preview_btn.setVisibility(View.GONE);
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
});
在这里,当我点击预览按钮时,它正在点击URL并从中获得响应。 但在警报对话框中,响应显示0.如果我取消对话框,我再次点击预览按钮,它给出了我想要的确切输出。 但我想在第一次点击时想要这个,请帮我解决这个问题。
答案 0 :(得分:0)
以下是 AsyncTask HTTP请求
的示例public class HttpGetRequest extends AsyncTask<String, Void, String> {
public static final String REQUEST_METHOD = "GET";
public static final int READ_TIMEOUT = 15000;
public static final int CONNECTION_TIMEOUT = 15000;
@Override
protected String doInBackground(String... params){
String stringUrl = params[0];
String result;
String inputLine;
try {
//Create a URL object holding our url
URL myUrl = new URL(stringUrl);
//Create a connection
HttpURLConnection connection =(HttpURLConnection)
myUrl.openConnection();
//Set methods and timeouts
connection.setRequestMethod(REQUEST_METHOD);
connection.setReadTimeout(READ_TIMEOUT);
connection.setConnectTimeout(CONNECTION_TIMEOUT);
//Connect to our url
connection.connect()
//Create a new InputStreamReader
InputStreamReader streamReader = new
InputStreamReader(connection.getInputStream());
//Create a new buffered reader and String Builder
BufferedReader reader = new BufferedReader(streamReader);
StringBuilder stringBuilder = new StringBuilder();
//Check if the line we are reading is not null
while((inputLine = reader.readLine()) != null){
stringBuilder.append(inputLine);
}
//Close our InputStream and Buffered reader
reader.close();
streamReader.close();
//Set our result equal to our stringBuilder
result = stringBuilder.toString();
}
catch(IOException e){
e.printStackTrace();
result = null;
}
return result;
}
protected void onPostExecute(String result){
super.onPostExecute(result);
// result is your result string. Do your logic here.
}
}
希望这可以帮助你。
了解更多信息
答案 1 :(得分:0)
您可以尝试以下代码:
final Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
try {
sms_content = URLEncoder.encode(sms_content, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
try {
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpPost = new HttpGet("http://v.leonas.in/events/register/credit.php?template=" + sms_content);
HttpResponse response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
String responseString = EntityUtils.toString(entity);
JSONObject jsonObject = new JSONObject(responseString);
creditspersms = jsonObject.getInt("credits");
System.out.println(creditspersms);
} catch (JSONException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
preview.setText(sms_content);
smscount.setText(String.valueOf(creditspersms * contnum));
numofcont.setText(Integer.toString(contnum));
// set dialog message
alertDialogBuilder
.setCancelable(false)
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// get user input and set it to result
// edit text
editText.setText(editText.getText());
send_btn.setVisibility(View.VISIBLE);
preview_btn.setVisibility(View.GONE);
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
});
}
});
thread.start();