https://www.sunofbeach.net/a/1203982957619040256
我完完全全按照上面贴出的笔记的代码写了,返回码还是400。最离谱的还是在这里我做的是三个按钮,第一个做的是post请求不带参数的OK了,到另外的两个按钮,get和posturl带参数的每次返回码都是400。手动@群主,哈哈哈
这是刚开启服务器程序时的:
这是进行了带参数的uri后的:
您的每一个用心回答,都会让这个世界变得更美好一些!
最后一张图不是说了嘛,编码问题呢。
上代码呀,你说的不算的。
还有你那个文件夹,路径呀。也有中文。这很容易出问题的。作为程序员,对这些要敏感一些。你最好能懂一些单词吧。英语不精通就算了,单词你总得知道几个。
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(); } }
最后一张图不是说了嘛,编码问题呢。
上代码呀,你说的不算的。
还有你那个文件夹,路径呀。也有中文。这很容易出问题的。作为程序员,对这些要敏感一些。你最好能懂一些单词吧。英语不精通就算了,单词你总得知道几个。