Java android未将任何内容发布到URL(无错误)
我有这样的代码:Java android未将任何内容发布到URL(无错误),java,android,Java,Android,我有这样的代码: public class MainActivity extends ActionBarActivity { EditText usernameEditText; EditText passwordEditText; public Button saveme; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(save
public class MainActivity extends ActionBarActivity {
EditText usernameEditText;
EditText passwordEditText;
public Button saveme;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
usernameEditText = (EditText) findViewById(R.id.username);
passwordEditText = (EditText) findViewById(R.id.pass);
saveme = (Button) findViewById(R.id.loginn);
saveme.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String givenUsername = usernameEditText.getEditableText().toString();
String givenPassword = passwordEditText.getEditableText().toString();
new LoginTask(MainActivity.this,givenUsername, givenPassword).execute();
// CALL GetText method to make post method call
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
class LoginTask extends AsyncTask<Void, Void, String>{
private String username;
private String password;
private Context context;
public LoginTask(Context context, String username, String password) {
this.username = username;
this.password = password;
this.context = context;
}
@Override
protected String doInBackground(Void... voids) {
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://facenegah.com/android/login.php");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("email", username));
nameValuePairs.add(new BasicNameValuePair("pass", password));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
String responseText = EntityUtils.toString(entity);
return responseText;
}
catch(Exception ex)
{
}
return null;
}
公共类MainActivity扩展了ActionBarActivity{
EditText用户名EditText;
EditText密码EditText;
公共按钮saveme;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
usernameEditText=(EditText)findViewById(R.id.username);
passwordEditText=(EditText)findViewById(R.id.pass);
saveme=(按钮)findviewbyd(R.id.loginn);
saveme.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
字符串givenUsername=usernameEditText.getEditableText().toString();
字符串givenPassword=passwordEditText.getEditableText().toString();
新建登录任务(MainActivity.this、givenUsername、givenPassword).execute();
//调用GetText方法进行post方法调用
}
});
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
//为菜单充气;这会将项目添加到操作栏(如果存在)。
getMenuInflater().充气(右菜单菜单菜单主菜单);
返回true;
}
@凌驾
公共布尔值onOptionsItemSelected(菜单项项){
//处理操作栏项目单击此处。操作栏将
//自动处理Home/Up按钮上的点击,只要
//在AndroidManifest.xml中指定父活动时。
int id=item.getItemId();
//noinspection SimplifiableIf语句
if(id==R.id.action\u设置){
返回true;
}
返回super.onOptionsItemSelected(项目);
}
}
类LoginTask扩展了异步任务{
私有字符串用户名;
私有字符串密码;
私人语境;
公共登录任务(上下文、字符串用户名、字符串密码){
this.username=用户名;
this.password=密码;
this.context=上下文;
}
@凌驾
受保护的字符串背景(无效…无效){
试一试{
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(“http://facenegah.com/android/login.php");
List nameValuePairs=新的ArrayList(2);
添加(新的BasicNameValuePair(“电子邮件”,用户名));
添加(新的BasicNameValuePair(“pass”,password));
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
HttpResponse response=httpclient.execute(httppost);
HttpEntity=response.getEntity();
String responseText=EntityUtils.toString(实体);
返回响应文本;
}
捕获(例外情况除外)
{
}
返回null;
}
它没有抛出任何错误,运行良好,没有强制关闭。但是,它没有向URL发布任何内容,甚至没有打开它
为什么会这样?有人能帮我吗?而不是
String responseText = EntityUtils.toString(entity);
尝试:
私有字符串convertResponseToString(HttpResponse响应,
HttpEntity(实体){
字符串resp=“”;
试一试{
InputStream InputStream=entity.getContent();
if(response.containsHeader(“内容编码”)
&&response.getLastHeader(“内容编码”).toString()
.contains(“gzip”)){
GZIPInputStream gzip=新的GZIPInputStream(inputStream);
StringBuffer szBuffer=新的StringBuffer();
字节tByte[]=新字节[1024];
while(true){
int-ilelength=gzip.read(t字节,0,1024);
如果(i长度<0)
打破
append(新字符串(tByte,0,iLength));
}
resp=szBuffer.toString();
}否则{
BufferedReader in=新的BufferedReader(新的InputStreamReader(
输入流);
StringBuffer responseString=新建StringBuffer();
字符串temp=null;
而((temp=in.readLine())!=null){
响应预算追加(临时);
}
resp=responseString.toString();
}
}捕获(例外e){
e、 printStackTrace();
}
返回响应;
}
我明白了。这是因为我用小写字母编写了权限类型
应该是:
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
检查你的服务器端,看看他们是如何捕获请求参数的……我在php上工作了5年……我检查日志,甚至那个文件,打开它对数据库的插入查询,即使它没有任何POST或GET内容,但这个安卓代码甚至没有数据打开它06-22 13:17:02.864 1243-1296/系统处理W/A乌迪奥特拉克﹕ 音频输出被客户端快速拒绝我在控制台安卓按下按钮时出现此错误我想你应该尝试一下。因为我用小写字母写权限:|很好,你找到了原因。这怎么没有引发安全异常呢?我不知道,我在用安卓5 SDK
private String convertResponseToString(HttpResponse response,
HttpEntity entity) {
String resp = "";
try {
InputStream inputStream = entity.getContent();
if (response.containsHeader("Content-Encoding")
&& response.getLastHeader("Content-Encoding").toString()
.contains("gzip")) {
GZIPInputStream gzip = new GZIPInputStream(inputStream);
StringBuffer szBuffer = new StringBuffer();
byte tByte[] = new byte[1024];
while (true) {
int iLength = gzip.read(tByte, 0, 1024);
if (iLength < 0)
break;
szBuffer.append(new String(tByte, 0, iLength));
}
resp = szBuffer.toString();
} else {
BufferedReader in = new BufferedReader(new InputStreamReader(
inputStream));
StringBuffer responseString = new StringBuffer();
String temp = null;
while ((temp = in.readLine()) != null) {
responseString.append(temp);
}
resp = responseString.toString();
}
} catch (Exception e) {
e.printStackTrace();
}
return resp;
}
<uses-permission android:name="android.permission.INTERNET"></uses-permission>