Skip to content

Commit

Permalink
代码提交
Browse files Browse the repository at this point in the history
一款封装HttpURLConnection实现的简单的网络请求的事例,提供了对应的apk和源码以及调用事例。暂时放上源码,后续提供代码介绍
  • Loading branch information
bihe0832 committed Dec 28, 2016
0 parents commit 7f98f31
Show file tree
Hide file tree
Showing 70 changed files with 2,765 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

*.gradle

*.idea


*.iml

build
23 changes: 23 additions & 0 deletions Advanced/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.bihe0832.request.demo.advanced" >

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme" >
<activity android:name="com.bihe0832.request.demo.MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package com.bihe0832.request.demo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.bihe0832.request.demo.advanced.R;
import com.bihe0832.request.demo.request.GetRequest;
import com.bihe0832.request.demo.request.PostRequest;
import com.bihe0832.request.demo.request.TestResponse;
import com.bihe0832.request.libware.request.HTTPServer;
import com.bihe0832.request.libware.request.HttpResponseHandler;

public class MainActivity extends AppCompatActivity {


private EditText mRequestParaEditText;
private TextView mResultView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mRequestParaEditText = (EditText)findViewById(R.id.request_paraEditText);
mResultView = (TextView)findViewById(R.id.request_resultTextView);

Button getRequestButton = (Button) findViewById(R.id.request_getBtn);
getRequestButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
sendGetRequest();
}
});

Button postRequestButton = (Button) findViewById(R.id.request_postBtn);
postRequestButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
sendPostRequest();
}
});

Button clearResultButton = (Button) findViewById(R.id.request_clear);
clearResultButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mResultView.setText("");
}
});
}

private void sendGetRequest(){
String para = mRequestParaEditText.getText().toString();
if(null != para && para.length() > 0){
TestResponesHandler handle = new TestResponesHandler();
GetRequest request = new GetRequest(para,handle);
HTTPServer.getInstance().doRequest(request);
}else{
showResult("请在输入框输入请求内容!");
}
}

private void sendPostRequest(){
String para = mRequestParaEditText.getText().toString();
if(null != para && para.length() > 0){
TestResponesHandler handle = new TestResponesHandler();
PostRequest request = new PostRequest(para,handle);
HTTPServer.getInstance().doRequest(request);
}else{
showResult("请在输入框输入请求内容!");
}
}

private class TestResponesHandler implements HttpResponseHandler<TestResponse> {

@Override
public void onResponse(TestResponse response) {
showResult("ret:\n\t" + response.ret + " \n " +
"msg:\n\t" + response.msg+ " \n " +
"para:\n\t" + response.para+ " \n ");
}
}

private void showResult(final String tips){
runOnUiThread(new Runnable() {
@Override
public void run() {
mResultView.setText(tips);
}
});
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.bihe0832.request.demo.request;


import com.bihe0832.request.libware.request.HttpRequest;
import com.bihe0832.request.libware.request.HttpResponseHandler;

import org.json.JSONObject;

public class GetRequest extends HttpRequest {

private static final String LOG_TAG = "bihe0832 REQUEST";


public static final String PATH = "/AndroidHTTP/get.php";
public static final String PARA_PARA = "para";

private String mPara = "";

private HttpResponseHandler<TestResponse> mResponseHandlerHandler;

public GetRequest(String para, HttpResponseHandler<TestResponse> handler) {
super(PATH);
this.mPara = para;
this.mResponseHandlerHandler = handler;
}

@Override
public String getUrl() {
StringBuilder builder = new StringBuilder();
builder.append(PARA_PARA + HTTP_REQ_ENTITY_MERGE + mPara);
return getBaseUrl()+"?"+builder.toString();
}

@Override
protected void onRequestSuccess(int statusCode, JSONObject response) {
TestResponse responses = new TestResponse();
responses.parseSuccessResponse(statusCode, response);
if (mResponseHandlerHandler != null) {
mResponseHandlerHandler.onResponse(responses);
}
}

@Override
protected void onRequestFailure(int statusCode, String errorResponse) {
TestResponse responses = new TestResponse();
responses.parseFailureResponse(statusCode, errorResponse);
if (mResponseHandlerHandler != null) {
mResponseHandlerHandler.onResponse(responses);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.bihe0832.request.demo.request;


import com.bihe0832.request.libware.TextUtils;
import com.bihe0832.request.libware.request.HttpRequest;
import com.bihe0832.request.libware.request.HttpResponseHandler;

import org.json.JSONObject;

public class PostRequest extends HttpRequest {

private static final String LOG_TAG = "bihe0832 REQUEST";


public static final String PATH = "/AndroidHTTP/post.php";
public static final String PARA_PARA = "para";

private HttpResponseHandler<TestResponse> mResponseHandlerHandler;

public PostRequest(String para, HttpResponseHandler<TestResponse> handler) {
super(PATH);
String encodedParam = PARA_PARA + HTTP_REQ_ENTITY_MERGE + para;
this.data = TextUtils.getBytesUTF8(encodedParam);
this.mResponseHandlerHandler = handler;
}

@Override
public String getUrl() {
return getBaseUrl();
}

@Override
protected void onRequestSuccess(int statusCode, JSONObject response) {
TestResponse responses = new TestResponse();
responses.parseSuccessResponse(statusCode, response);
if (mResponseHandlerHandler != null) {
mResponseHandlerHandler.onResponse(responses);
}
}

@Override
protected void onRequestFailure(int statusCode, String errorResponse) {
TestResponse responses = new TestResponse();
responses.parseFailureResponse(statusCode, errorResponse);
if (mResponseHandlerHandler != null) {
mResponseHandlerHandler.onResponse(responses);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.bihe0832.request.demo.request;


import android.util.Log;

import com.bihe0832.request.libware.request.HTTPServer;
import com.bihe0832.request.libware.request.HttpResponse;

import org.json.JSONException;
import org.json.JSONObject;


public class TestResponse extends HttpResponse {

private static final String LOG_TAG = "bihe0832 REQUEST";
public String para = "";

public static final String PARA_PARA = "para";

@Override
public void parseJson(JSONObject json) {
super.parseBaseJson(json);
if (HTTPServer.RET_SUCC == ret) {
parseGuestUserCheckSuccRespones(json);
} else {
Log.w(LOG_TAG, json.toString());
}
}

private void parseGuestUserCheckSuccRespones(JSONObject json) {
try {
para = json.getString(PARA_PARA);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.bihe0832.request.libware;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class TextUtils {
/**
* 判断字符串是否为空
*
* @param s
* 需要判断的字符串
* @return boolean 为空返回true
*/
public static boolean ckIsEmpty(String s) {
if (s == null || s.trim().equals("") || s.trim().equals("null")) {
return true;
}
return false;
}

public static byte[] getBytesUTF8(String str) {
try {
return str.getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
return null;
}
}
}
Loading

0 comments on commit 7f98f31

Please sign in to comment.