0
  • 最佳答案

    最后一张图不是说了嘛,编码问题呢。

    1153952789488054272  评论     打赏       拉大锯
    • 上代码呀,你说的不算的。

      1153952789488054272  评论     打赏       拉大锯
      • 粟子啊  回复 @拉大锯
        来啦来啦哈哈哈
        粟子啊 2020-05-05 20:27   回复 1232220247331704832
      • 黑羊一匹  回复 @粟子啊
        解决了吗?我也是遇到这个问题,把关键字改为英语就不会了。我是新手,百度怎么搜也搜不到方法解决中文的问题
        黑羊一匹 2020-06-05 10:49   回复 1267104227352645632
      • 黑羊一匹  回复 @粟子啊
        把 map.put("keyword", "这是我的关键字")改为map.put("keyword",toUtf8String("这是我的关键字"));
        黑羊一匹 2020-06-05 10:58   回复 1267104227352645632
      • 黑羊一匹  回复 @黑羊一匹
        toUtf8String函数在上面的图片
        黑羊一匹 2020-06-05 11:01   回复 1267104227352645632
      • 粟子啊  回复 @黑羊一匹
        谢谢哈 已经解决啦。就是和你说的,得改成英文。棒棒棒的!
        粟子啊 2020-06-06 08:33   回复 1232220247331704832

    • 1267104227352645632  评论     打赏       黑羊一匹
      • 还有你那个文件夹,路径呀。也有中文。这很容易出问题的。作为程序员,对这些要敏感一些。你最好能懂一些单词吧。英语不精通就算了,单词你总得知道几个。

        1153952789488054272  评论     打赏       拉大锯
        • 粟子啊  回复 @拉大锯
          好的,谢谢师傅
          粟子啊 2020-05-06 18:48   回复 1232220247331704832
        • 拉大锯  回复 @粟子啊
          你解决了?看把你累得,这么费劲。还好这股劲不错。
          拉大锯 2020-05-06 18:49   回复 1153952789488054272
        • 粟子啊  回复 @拉大锯
          jar的路径我改成了纯英文,不带空格,并且关掉全部重新来了一遍,还是报的一样的错,我太难了0.0
          粟子啊 2020-05-06 18:56   回复 1232220247331704832
        • 拉大锯  回复 @粟子啊
          我现在不知道你是图片的问题,还是携带参数的问题。如果是图片的问题,你另外提一个问题。别整到一起了。
          拉大锯 2020-05-06 19:04   回复 1153952789488054272
      • 1232220247331704832  评论     打赏       粟子啊
        • 拉大锯  回复 @粟子啊
          405就是,方法错误。浏览器状态栏的是Get请求,你用postMan进行测试。不要选错了,有选项的,选择对应的请求方法。
          拉大锯 2020-05-05 22:34   回复 1153952789488054272
        • 粟子啊  回复 @拉大锯
          我看了下我不会整那个。。。我在网上查了,说是可能我提交参数的类型和后端的类型不一致
          粟子啊 2020-05-06 00:07   回复 1232220247331704832
        • 粟子啊  回复 @粟子啊
          http://192.168.0.101:9102/get/param?order=1&page=66&keyword=这是我的关键字 这个url放在浏览器正常访问到并且有结果返回,但是在AS里运行返回码就是400
          粟子啊 2020-05-06 00:18   回复 1232220247331704832
        • 拉大锯  回复 @粟子啊
          那就是你代码问题咯。对着视频都写不出来么?[捂脸]
          拉大锯 2020-05-06 01:45   回复 1153952789488054272
        • 粟子啊  回复 @拉大锯
          呜呜呜!估计我粗心了。晚点我复制笔记里的全部过去试试再来哈哈哈哈
          粟子啊 2020-05-06 08:55   回复 1232220247331704832
        • 粟子啊  回复 @拉大锯
          我把笔记里的全部代码复制粘贴了,并且前后检查了三遍绝对和笔记一样了,可在url带参数的get和post时返回码还是400,为了检验是不是我url填错了,我又多加了一个按钮,那个按钮是不带参数的post请求,结果成功了,返回码200。太离谱 了这。。。。
          粟子啊 2020-05-06 12:11   回复 1232220247331704832
        • 拉大锯  回复 @粟子啊
          服务器程序有没有Log输出呀。
          拉大锯 2020-05-06 12:43   回复 1153952789488054272
        • 粟子啊  回复 @拉大锯
          是指Powershell窗口吗。
          粟子啊 2020-05-06 18:17   回复 1232220247331704832
        • 拉大锯  回复 @粟子啊
          嗯,是的。
          拉大锯 2020-05-06 18:20   回复 1153952789488054272
        • 粟子啊  回复 @拉大锯
          我现在就去截图
          粟子啊 2020-05-06 18:21   回复 1232220247331704832
        • 拉大锯  回复 @粟子啊
          你补充到问题后面吧。
          拉大锯 2020-05-06 18:21   回复 1153952789488054272
        • 粟子啊  回复 @拉大锯
          我在补充问题那里上传图片不知道为什么老是失败,上传不了
          粟子啊 2020-05-06 18:33   回复 1232220247331704832
        • 粟子啊  回复 @拉大锯
          OK啦 我重新补充问题了
          粟子啊 2020-05-06 18:36   回复 1232220247331704832
        • 拉大锯  回复 @粟子啊
          图片应该是因为你的路径问题,jar包程序你要放在一个没有中文,没有空格的目录下。
          拉大锯 2020-05-06 18:42   回复 1153952789488054272
        • 粟子啊  回复 @拉大锯
          哈哈哈(好像发表情),解决啦师傅,辛苦了!是参数值的问题,前面我照着笔记里的值“这是我的关键字”,就会导致400错误,但我现在换成了纯英文的值"haha",通过了,返回码200,有结果返回
          粟子啊 2020-05-06 19:09   回复 1232220247331704832
        • 粟子啊  回复 @拉大锯
          这是啥原因呀,视频里师傅演示的是用中文都通过了,我的一模一样的中文竟然没通过
          粟子啊 2020-05-06 19:10   回复 1232220247331704832
        • 拉大锯  回复 @粟子啊
          我代码写得不好,不够严谨。当时也在常切换,太累了。一会写后台,一会写android。还得写博客,录视频。有很多细节没做好。
          拉大锯 2020-05-06 19:32   回复 1153952789488054272
        • 粟子啊  回复 @拉大锯
          已经做的很好很棒了哈哈哈,反正我学到了很多东西,感谢!!(此处应该有表情哈哈)
          粟子啊 2020-05-07 16:27   回复 1232220247331704832
        • 拉大锯  回复 @粟子啊
          感觉咱们在这聊天了,回到了15年前的BBS。
          拉大锯 2020-05-07 16:55   回复 1153952789488054272
      • public class PostTextActivity extends AppCompatActivity {
        
            private static final String TAG = "PostTextActivity";
            private OutputStream mOutputStream = null;
            private InputStream mInputStream = null;
            private Gson mGson;
            private final String BASE_URL = "http://192.168.0.101:9102";
            BufferedReader bufferedReader = null;
        
            @Override
            protected void onCreate(@Nullable Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.post_text_activity);
            }
        
            public void PostText(View view) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            URL url = new URL(BASE_URL + "/post/comment");
                            Log.d(TAG, "run: "+url);
                            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                            httpURLConnection.setConnectTimeout(2000);
                            httpURLConnection.setRequestMethod("POST");
                            //设置请求头
                            httpURLConnection.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
                            httpURLConnection.setRequestProperty("Accept-Language", "zh-CN,zh;q=0.9");
                            httpURLConnection.setRequestProperty("Accept", "application/json, text/plain, */*");
        
                            CommentItem commentItem = new CommentItem("6688", "越努力越幸运,一定要幸运!!!");
                            mGson = new Gson();
                            String i = mGson.toJson(commentItem);
                            byte[] bytes = i.getBytes("UTF-8");
                            httpURLConnection.setRequestProperty("Content-Length", String.valueOf(bytes.length));
                            Log.d(TAG, "run: " + bytes.length);
        
                            //连接网络。请求行,请求头的设置必须放在网络连接前
                            httpURLConnection.connect();
                            //把我们想发送给服务端的数据发送给服务
                            mOutputStream = httpURLConnection.getOutputStream();
                            //通过字符串的getBytes方法,把我们想发送给服务的(字符串)数据,转换成为一个byte数组
        
                            mOutputStream.write(bytes);
                            //只有调用了flush(),才能刷新此输出流并强制写出所有缓冲的输出字节。
                            mOutputStream.flush();
                            //拿到我们post请求的结果
                            int responseCode = httpURLConnection.getResponseCode();
                            if (responseCode == HttpURLConnection.HTTP_OK) {
                                mInputStream = httpURLConnection.getInputStream();
                                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(mInputStream));
                                String s = bufferedReader.readLine();
                                if (s != null) {
                                    Log.d(TAG, "run: " + s);
                                }
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        } finally {
                            if (mOutputStream != null) {
                                try {
                                    mOutputStream.close();
                                } catch (IOException e) {
                                    e.printStackTrace();
                                }
                            }
                            if (mInputStream != null) {
                                try {
                                    mInputStream.close();
                                } catch (IOException e) {
                                    e.printStackTrace();
                                }
                            }
                        }
                    }
                }).start();
        
            }
        
            //post请求带参数
            public void postTextParameter(View view) {
                //准备好所需参数
                Map<String, String> map = new HashMap<>();
                //key的值要和服务端那边给到的值一致好像
                map.put("string", "这是我提交的字符串");
                startRequest(map, "POST", "/post/string");
            }
        
            //get请求带参数
            public void getTextParameter(View view) {
                //准备好所需参数
                Map<String, String> map = new HashMap<>();
                //key的值要和服务端那边给到的值一致好像
                map.put("keyword", "这是我的关键字");
                map.put("page", "66");
                map.put("order", "1");
                startRequest(map, "GET", "/get/param");
            }
        
            private void startRequest(final Map<String, String> parans, final String method, final String api) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            //组装这次请求所需的参数
                            StringBuffer sb = new StringBuffer();
                            if (parans != null && parans.size() > 0) {
                                sb.append("?");
                                //通过entrySet获得到一个Set集合,这个集合里面的类型为Map.Entry,Map.Entry是Map声明的一个内部接口,
                                // 此接口为泛型,定义为Entry<K,V>。它表示的是Map中的一个实体(key-value)。接口中有我们常用的getKey(),getValue方法。
                                Set<Map.Entry<String, String>> entries = parans.entrySet();
                                //通过iterator方法返回在此 set 中的元素上进行迭代的迭代器。
                                Iterator<Map.Entry<String, String>> iterator = entries.iterator();
        
                                //Iterator的hasNext方法,如果仍有元素可以迭代,则返回 true。
                                while (iterator.hasNext()) {
                                    //iterator迭代的是Map.Entry里的内容,而Map.Entry里存放的又是Map中的一对键值对,所以有getKey()等方法
                                    Map.Entry<String, String> next = iterator.next();
                                    sb.append(next.getKey());
                                    sb.append("=");
                                    sb.append(next.getValue());
                                    //上面已经把参数和其值找到了,通过iterator.hasNext()判断是否有多个参数,如果是,则用&符号分隔每一个参数
                                    if (iterator.hasNext()) {
                                        sb.append("&");
                                    }
                                }
                                Log.d(TAG, "startRequest: " + sb.toString());
                            }
                            String string = sb.toString();
                            URL url;
                            if (string != null && string.length() > 0) {
                                url = new URL(BASE_URL + api + string);
                            } else {
                                url = new URL(BASE_URL + api);
                            }
                            Log.d(TAG, "startRequest: " + url.toString());
                            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                            httpURLConnection.setRequestMethod(method);
                            httpURLConnection.setConnectTimeout(2000);
                            httpURLConnection.setRequestProperty("accept", "*/*");
                            httpURLConnection.setRequestProperty("connection", "keep-alive");
                            httpURLConnection.setRequestProperty("Accept-Language", "zh-CN,zh");
                            httpURLConnection.connect();
                            int responseCode = httpURLConnection.getResponseCode();
                            Log.d(TAG, "run: "+responseCode);
                            if (responseCode == HttpURLConnection.HTTP_OK) {
                                InputStream inputStream = httpURLConnection.getInputStream();
                                bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
                                String readLine = bufferedReader.readLine();
                                Log.d(TAG, "startRequest123: " + readLine);
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        } finally {
                            if (bufferedReader != null) {
                                try {
                                    bufferedReader.close();
                                } catch (IOException e) {
                                    e.printStackTrace();
                                }
                            }
                        }
                    }
                }).start();
            }
        }
        


        1232220247331704832  评论     打赏       粟子啊
        • 拉大锯  回复 @粟子啊
          把有问题的代码整上来就好了。这好办,你不是有Log输出吗?复制你的log里的url,使用PostMan,或者扔到浏览器里,访问一下不就知道是你的代码问题,还是url的问题吗?一般来说,400,要么接口的参数错了/接口错了,要么编码问题。你可以尝试一下用纯英文的。
          拉大锯 2020-05-05 20:49   回复 1153952789488054272
        • 粟子啊  回复 @拉大锯
          我把url复制到浏览器访问 界面在截图给您看看
          粟子啊 2020-05-05 21:41   回复 1232220247331704832
        • 粟子啊  回复 @拉大锯
          Whitelabel Error Page This application has no explicit mapping for /error, so you are seeing this as a fallback. Tue May 05 21:49:53 CST 2020 There was an unexpected error (type=Method Not Allowed,
          粟子啊 2020-05-05 22:00   回复 1232220247331704832
        • 粟子啊  回复 @拉大锯
          通过url在浏览器访问是这样 我有点事 没能截图 不行明天我补上哈哈哈
          粟子啊 2020-05-05 22:01   回复 1232220247331704832
        • 粟子啊  回复 @拉大锯
          我把图片发在上面了 群主有时间的话帮忙看看是啥回事
          粟子啊 2020-05-05 22:30   回复 1232220247331704832
      相关问题
      独一无二的名字2020 · Android网络编程
      2020-03-26 20:11 501 2
      独一无二的臭弟弟 · 网络编程出问题
      2020-04-29 07:42 423 4
      2020-05-19 05:35 437 2
      刘小睿 · 网络编程
      2020-10-01 01:08 406 2
      2020-10-13 00:04 367 2
      斗鱼、芜湖大司马 · 网络编程 / Android
      2021-04-27 19:11 433 4
      Snooker · 网络编程
      2021-11-10 23:34 405 2