替代方案是在android中使用URLConnection
我认为android中的替代方案是在android中使用URLConnection,android,Android,我认为android中的URLConnection有一个bug。我真的不知道。请参阅以下代码。这对我来说很奇怪。我从服务器发送hello流,当android获得hello流时,它首先将hello流放入textView中,但它无法运行代码中的真实条件,假设php中的hello流等于androidcode中的hello流 public class MainActivity extends Activity { TextView textView; @Override protect
URLConnection
有一个bug。我真的不知道。请参阅以下代码。这对我来说很奇怪。我从服务器发送hello
流,当android获得hello
流时,它首先将hello
流放入textView
中,但它无法运行代码中的真实条件,假设php
中的hello
流等于android
code中的hello
流
public class MainActivity extends Activity {
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.textView);
GetText();
}
void GetText(){
String text = "";
BufferedReader reader=null;
try
{
// Defined URL where to send data
URL url = new URL("http://127.0.0.1:8080/apps/request.php");
// Send POST data request
URLConnection conn = url.openConnection();
// Get the server response
reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuilder sb = new StringBuilder();
String line = null;
// Read Server Response
while((line = reader.readLine()) != null)
{
// Append server response in string
sb.append(line + "\n");
}
text = sb.toString();
}
catch(Exception ex)
{
}
finally
{
try
{
reader.close();
}
catch(Exception ex) {}
}
// Show response on activity
textView.setText( text );
if(text.equals("hello"))
Toast.makeText(getApplicationContext(),"Yes",Toast.LENGTH_LONG).show();
else Toast.makeText(getApplicationContext(), "No",Toast.LENGTH_LONG).show();
}}
解决这个问题对我来说很重要。我应该根据从php
文件发送的数据执行差异操作。但是我的代码运行的操作是不完整的
// Show response on activity
textView.setText( text );//My code can set the text from php in the textView.
if(text.equals("hello"))
//My code can not display the `Yes` stream although the contents of the text variable is equal to the `hello` stream.
Toast.makeText(getApplicationContext(),"Yes",Toast.LENGTH_LONG).show();
My code displays the `No` stream and it is very strange for me.
else Toast.makeText(getApplicationContext(), "
No",Toast.LENGTH_LONG).show();
真的没有一个解决方案或替代方案适合我吗
<?php
echo "hello";
?>
HttpClient-HttpClient=newdefaulthttpclient();
HttpPost HttpPost=新的HttpPost(URL);
List nameValuePairs=新的ArrayList();
添加(新的BasicNameValuePair(“parm1”,参数[0]);
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
HttpResponse response=httpclient.execute(httppost);
BufferedReader br=新的BufferedReader(新的InputStreamReader((response.getEntity().getContent()));
字符串输出;
StringBuilder responseJsonStr=新建StringBuilder();
而((output=br.readLine())!=null){
responseJsonStr.append(输出);
}
如果(!StringUtils.startsWith(responseJsonStr.toString(),“[”)){
responseJsonStr.插入(0,“[”);
responseJsonStr.追加(“]”);
}
我找到了问题的原因。我将我的php
文件从编码:UTF-8
更改为编码:ANSI
,现在我可以很好地运行我的代码了。您是否使用text.trim().equals(“hello”)进行了尝试
?从流中读取时,您正在追加一个\n
。即使我删除\n
并使用text.trim().equals(“hello”)
,也存在此问题。您的解决方案(HttpClient
)已被弃用。
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(URL);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("parm1", params[0]));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
BufferedReader br = new BufferedReader(new InputStreamReader((response.getEntity().getContent())));
String output;
StringBuilder responseJsonStr = new StringBuilder();
while ((output = br.readLine()) != null) {
responseJsonStr.append(output);
}
if(!StringUtils.startsWith(responseJsonStr.toString(), "[")) {
responseJsonStr.insert(0,"[");
responseJsonStr.append("]");
}