Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/205.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/131.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android HttpUrlConnection文件NotFoundException。连接到http并传递参数 公共类地理注册扩展异步任务{ 私人活动; 私有静态最终字符串USERNAME=“USERNAME=”; 私有静态最终字符串EMAIL=“&EMAIL=”; 私有静态最终字符串PASSWORD=“&PASSWORD=”; 私有静态最终字符串PASSWORD_REPEAT=“&PASSWORD_REPEAT=“”; 专用字符串路由; 私有字符串参数序列; 公共地理注册(){ } 公共地理注册(活动活动、字符串用户名、字符串电子邮件、字符串密码、字符串密码确认){ 这个。活动=活动; this.route=activity.getResources().getString(R.string.domain)+activity.getResources().getString(R.string.registration\u second\u step\u route); this.paramsSequence=用户名+用户名+ 电子邮件+电子邮件+ 密码+密码+ 密码\重复+密码确认; } @凌驾 受保护的void onPreExecute(){ super.onPreExecute(); } @凌驾 受保护的授权响应容器doInBackground(无效…参数){ 字符串消息=null; int代码=0; HttpURLConnection HttpURLConnection=null; 试一试{ URL=新URL(路由); httpURLConnection=(httpURLConnection)url.openConnection(); httpURLConnection.setReadTimeout(10000); httpURLConnection.setConnectTimeout(15000); httpURLConnection.setRequestMethod(“POST”);//有时(很奇怪,我发现ProtocolExeption:连接已经建立) httpURLConnection.setDoInput(true); httpURLConnection.setDoOutput(true); /*PrintWriter out=新的PrintWriter(httpURLConnection.getOutputStream()); 输出。打印(此参数序列); out.close()*/ OutputStream os=httpURLConnection.getOutputStream(); OutputStreamWriter osw=新的OutputStreamWriter(os); osw.write(参数序列); osw.flush(); osw.close(); httpURLConnection.connect(); int status=httpURLConnection.getResponseCode(); BufferedReader=new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream());//FileNotFounException StringBuilder=新的StringBuilder(); 弦线; 而((line=reader.readLine())!=null){ builder.append(第+行“\n”); } JSONObject JSONObject=新的JSONObject(builder.toString()); message=jsonObject.getString(ResponseKeys.message); code=jsonObject.getInt(ResponseKeys.code); 开关(状态){ 案例200: 如果(代码==0){ } 返回null; 案例400: 返回新的AuthorizationResponseContainer(ResponseKeys.CODE_400,代码,消息,空); 案例500: 返回新的AuthorizationResponseContainer(ResponseKeys.CODE_500,CODE,message,null); } }捕获(IOException | JSONException e){ e、 printStackTrace(); }最后{ if(httpURLConnection!=null){ 试一试{ httpURLConnection.disconnect(); }捕获(例外情况除外){ 例如printStackTrace(); } } } 返回null; }_Android_Post_Httpurlconnection_Filenotfoundexception - Fatal编程技术网

Android HttpUrlConnection文件NotFoundException。连接到http并传递参数 公共类地理注册扩展异步任务{ 私人活动; 私有静态最终字符串USERNAME=“USERNAME=”; 私有静态最终字符串EMAIL=“&EMAIL=”; 私有静态最终字符串PASSWORD=“&PASSWORD=”; 私有静态最终字符串PASSWORD_REPEAT=“&PASSWORD_REPEAT=“”; 专用字符串路由; 私有字符串参数序列; 公共地理注册(){ } 公共地理注册(活动活动、字符串用户名、字符串电子邮件、字符串密码、字符串密码确认){ 这个。活动=活动; this.route=activity.getResources().getString(R.string.domain)+activity.getResources().getString(R.string.registration\u second\u step\u route); this.paramsSequence=用户名+用户名+ 电子邮件+电子邮件+ 密码+密码+ 密码\重复+密码确认; } @凌驾 受保护的void onPreExecute(){ super.onPreExecute(); } @凌驾 受保护的授权响应容器doInBackground(无效…参数){ 字符串消息=null; int代码=0; HttpURLConnection HttpURLConnection=null; 试一试{ URL=新URL(路由); httpURLConnection=(httpURLConnection)url.openConnection(); httpURLConnection.setReadTimeout(10000); httpURLConnection.setConnectTimeout(15000); httpURLConnection.setRequestMethod(“POST”);//有时(很奇怪,我发现ProtocolExeption:连接已经建立) httpURLConnection.setDoInput(true); httpURLConnection.setDoOutput(true); /*PrintWriter out=新的PrintWriter(httpURLConnection.getOutputStream()); 输出。打印(此参数序列); out.close()*/ OutputStream os=httpURLConnection.getOutputStream(); OutputStreamWriter osw=新的OutputStreamWriter(os); osw.write(参数序列); osw.flush(); osw.close(); httpURLConnection.connect(); int status=httpURLConnection.getResponseCode(); BufferedReader=new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream());//FileNotFounException StringBuilder=新的StringBuilder(); 弦线; 而((line=reader.readLine())!=null){ builder.append(第+行“\n”); } JSONObject JSONObject=新的JSONObject(builder.toString()); message=jsonObject.getString(ResponseKeys.message); code=jsonObject.getInt(ResponseKeys.code); 开关(状态){ 案例200: 如果(代码==0){ } 返回null; 案例400: 返回新的AuthorizationResponseContainer(ResponseKeys.CODE_400,代码,消息,空); 案例500: 返回新的AuthorizationResponseContainer(ResponseKeys.CODE_500,CODE,message,null); } }捕获(IOException | JSONException e){ e、 printStackTrace(); }最后{ if(httpURLConnection!=null){ 试一试{ httpURLConnection.disconnect(); }捕获(例外情况除外){ 例如printStackTrace(); } } } 返回null; }

Android HttpUrlConnection文件NotFoundException。连接到http并传递参数 公共类地理注册扩展异步任务{ 私人活动; 私有静态最终字符串USERNAME=“USERNAME=”; 私有静态最终字符串EMAIL=“&EMAIL=”; 私有静态最终字符串PASSWORD=“&PASSWORD=”; 私有静态最终字符串PASSWORD_REPEAT=“&PASSWORD_REPEAT=“”; 专用字符串路由; 私有字符串参数序列; 公共地理注册(){ } 公共地理注册(活动活动、字符串用户名、字符串电子邮件、字符串密码、字符串密码确认){ 这个。活动=活动; this.route=activity.getResources().getString(R.string.domain)+activity.getResources().getString(R.string.registration\u second\u step\u route); this.paramsSequence=用户名+用户名+ 电子邮件+电子邮件+ 密码+密码+ 密码\重复+密码确认; } @凌驾 受保护的void onPreExecute(){ super.onPreExecute(); } @凌驾 受保护的授权响应容器doInBackground(无效…参数){ 字符串消息=null; int代码=0; HttpURLConnection HttpURLConnection=null; 试一试{ URL=新URL(路由); httpURLConnection=(httpURLConnection)url.openConnection(); httpURLConnection.setReadTimeout(10000); httpURLConnection.setConnectTimeout(15000); httpURLConnection.setRequestMethod(“POST”);//有时(很奇怪,我发现ProtocolExeption:连接已经建立) httpURLConnection.setDoInput(true); httpURLConnection.setDoOutput(true); /*PrintWriter out=新的PrintWriter(httpURLConnection.getOutputStream()); 输出。打印(此参数序列); out.close()*/ OutputStream os=httpURLConnection.getOutputStream(); OutputStreamWriter osw=新的OutputStreamWriter(os); osw.write(参数序列); osw.flush(); osw.close(); httpURLConnection.connect(); int status=httpURLConnection.getResponseCode(); BufferedReader=new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream());//FileNotFounException StringBuilder=新的StringBuilder(); 弦线; 而((line=reader.readLine())!=null){ builder.append(第+行“\n”); } JSONObject JSONObject=新的JSONObject(builder.toString()); message=jsonObject.getString(ResponseKeys.message); code=jsonObject.getInt(ResponseKeys.code); 开关(状态){ 案例200: 如果(代码==0){ } 返回null; 案例400: 返回新的AuthorizationResponseContainer(ResponseKeys.CODE_400,代码,消息,空); 案例500: 返回新的AuthorizationResponseContainer(ResponseKeys.CODE_500,CODE,message,null); } }捕获(IOException | JSONException e){ e、 printStackTrace(); }最后{ if(httpURLConnection!=null){ 试一试{ httpURLConnection.disconnect(); }捕获(例外情况除外){ 例如printStackTrace(); } } } 返回null; },android,post,httpurlconnection,filenotfoundexception,Android,Post,Httpurlconnection,Filenotfoundexception,路线: 为什么我不能从HttpUrlConnection获取输入流?我捕获了FileNotFoundException。但当我在浏览器的REST API客户端执行相同操作时,我可以连接到主机并接收json应答。我做错了什么? 我被作为字符串传递http参数,需要作为JSON传递-这是第一次。 当服务器ANVER 4xx代码时-需要获取ErrorStream,而不是InputStream 例如: 我被作为字符串传递http参数,需要作为JSON——这是第一次。 当服务器ANVER 4xx代码时-需

路线: 为什么我不能从HttpUrlConnection获取输入流?我捕获了FileNotFoundException。但当我在浏览器的REST API客户端执行相同操作时,我可以连接到主机并接收json应答。我做错了什么?

我被作为字符串传递http参数,需要作为JSON传递-这是第一次。 当服务器ANVER 4xx代码时-需要获取ErrorStream,而不是InputStream 例如:


我被作为字符串传递http参数,需要作为JSON——这是第一次。 当服务器ANVER 4xx代码时-需要获取ErrorStream,而不是InputStream 例如:


添加这些代码行

int respCode = conn.getResponseCode();
Bufferreader reader;
switch(respCode){
case 200:
reader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream())); //to avoid FNFExeption
break;
case 400:
reader = new BufferedReader(new InputStreamReader(httpURLConnection.getErrorStream())); //to avoid FNFExeption
break;
}


我希望这将解决您的问题。谢谢…

添加这些代码行

int respCode = conn.getResponseCode();
Bufferreader reader;
switch(respCode){
case 200:
reader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream())); //to avoid FNFExeption
break;
case 400:
reader = new BufferedReader(new InputStreamReader(httpURLConnection.getErrorStream())); //to avoid FNFExeption
break;
}


我希望这能解决您的问题。谢谢…

删除httpURLConnection.connect();您已经连接并写入了输出流。删除httpURLConnection.connect();您已经连接并写入了输出流。
httpURLConnection.setRequestProperty("User-Agent","Mozilla/5.0 ( compatible ) ");
httpURLConnection.setRequestProperty("Accept", "*/*");
try {
    is = connection.getInputStream();
} catch(FileNotFoundException exception){
    log.error(exception.getMessage(), exception);
    is = connection.getErrorStream();
}
 BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));