forked from AnyMarvel/LianjiaSpider
-
Notifications
You must be signed in to change notification settings - Fork 50
/
MainActivity.java
executable file
·103 lines (87 loc) · 3.53 KB
/
MainActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package test.homelinke.com.homelinktest;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Base64;
import android.util.Log;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MainActivity extends AppCompatActivity {
final String source = "https://app.api.lianjia.com/house/chengjiao/search?city_id=110000&limit_offset=0&limit_count=100&request_ts=1511232061";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String encripty = encripty(source, null);
}
public String encripty(String str, Map<String, String> map) {
Map parmars = getParmars(str);
HashMap hashMap = new HashMap();
if (parmars != null) {
hashMap.putAll(parmars);
}
if (map != null) {
hashMap.putAll(map);
}
List arrayList = new ArrayList(hashMap.entrySet());
Collections.sort(arrayList, new Comparator() {
@Override
public int compare(Object obj, Object obj2) {
return a((Map.Entry) obj, (Map.Entry) obj2);
}
public int a(Map.Entry<String, String> entry, Map.Entry<String, String> entry2) {
return ((String) entry.getKey()).compareTo((String) entry2.getKey());
}
});
String GetAppSecret = "93273ef46a0b880faf4466c48f74878f";
String GetAppId = "20170324_android";
StringBuilder stringBuilder = new StringBuilder(GetAppSecret);
for (int i = 0; i < arrayList.size(); i++) {
Map.Entry entry = (Map.Entry) arrayList.get(i);
stringBuilder.append(((String) entry.getKey()) + "=" + ((String) entry.getValue()));
}
Log.d("ssssStringBuilder:", stringBuilder.toString());
Log.d("ssssSHA1:", Digest_SHA1(stringBuilder.toString()));
Log.d("ssss", GetAppId + ":" + Digest_SHA1(stringBuilder.toString()));
GetAppSecret = Base64.encodeToString((GetAppId + ":" + Digest_SHA1(stringBuilder.toString())).getBytes(), 2);
Log.d("sssss", GetAppSecret);
return new String("helloworld");
}
public static String Digest_SHA1(String str) {
try {
MessageDigest instance = MessageDigest.getInstance("SHA-1");
instance.update(str.getBytes());
byte[] digest = instance.digest();
StringBuffer stringBuffer = new StringBuffer();
for (byte b : digest) {
String toHexString = Integer.toHexString(b & 255);
if (toHexString.length() < 2) {
stringBuffer.append(0);
}
stringBuffer.append(toHexString);
}
return stringBuffer.toString();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
return "";
}
}
private Map<String, String> getParmars(String str) {
if (str == null || str.length() == 0) {
return null;
}
HashMap hashMap = new HashMap();
Uri parse = Uri.parse(str);
for (String str2 : parse.getQueryParameterNames()) {
String str22 = str2.toString();
hashMap.put(str22, parse.getQueryParameter(str22));
}
return hashMap;
}
}