分享按钮

This commit is contained in:
WMJ
2017-05-04 21:57:05 +08:00
parent eb4a3728e6
commit 00d97a164d
8 changed files with 204 additions and 1256 deletions
+1
View File
@@ -4,6 +4,7 @@
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
@@ -10,6 +10,7 @@ import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
@@ -26,12 +27,15 @@ import java.util.Map;
* 与网络相关的一些操作
*/
public class NetWork {
public class NetWork implements Runnable{
private static String host;
private static int port;
private static boolean connected;
public NetWork() {}
public boolean finished = false;
public String result;
public void setAddress(String s) {
NetWork.host = s;
}
@@ -162,17 +166,12 @@ public class NetWork {
return ans;
}
public void POST (String addr, HashMap<String, String> data) {
addr = "http://alphamj.cn/timemanager/share.php";
@Override
public void run() {
String addr = "http://alphamj.cn/timemanager/share.php";
HashMap<String, String> data = new HashMap<>();
try {
URL url = new URL(addr);
HttpURLConnection con = (HttpURLConnection)url.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("User-Agent", "Mozilla/5.0");
con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
data.put("user", "super");
data.put("count", "10");
@@ -187,19 +186,23 @@ public class NetWork {
data.put("8", "课8/7/7:00/8:00");
data.put("9", "课9/2/10:00/13:00");
data.put("10", "课10/3/10:00/12:00");
StringBuilder d = new StringBuilder();
for (Map.Entry<String, String> i : data.entrySet()) {
d.append(i.getKey() + "=" + i.getValue() + "&");
}
String sd = new String(d);
URL url = new URL(addr);
HttpURLConnection con = (HttpURLConnection)url.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("User-Agent", "Mozilla/5.0");
con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
con.setDoOutput(true);
con.setDoInput(true);
DataOutputStream o = new DataOutputStream(con.getOutputStream());
o.writeBytes(sd);
OutputStream o = con.getOutputStream();
o.write(sd.getBytes("UTF-8"));
o.flush();
o.close();
@@ -220,10 +223,12 @@ public class NetWork {
//打印结果
System.out.println(response.toString());
result = response.toString();
} catch (IOException e) {
result = "fail";
e.printStackTrace();
}
finished = true;
}
}
@@ -0,0 +1,98 @@
package wesayallright.timemanager.InnerLayer.Network;
import android.provider.Settings;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
/**
* Created by mj on 17-5-2.
* 发送POST请求
*/
public class SendPost implements Runnable{
public boolean finished;
public String result;
private HashMap<String, String> params;
private String addr;
public SendPost() {
finished = false;
result = "";
params = new HashMap<>();
addr = "http://alphamj.cn/timemanager/share.php";
}
public void setUrl(String url) {
this.addr = url;
}
public void addPrama(String key, String value) {
params.put(key, value);
}
public void addPrama(HashMap<String, String> m) {
params.putAll(m);
}
@Override
public void run() {
try {
StringBuilder d = new StringBuilder();
for (Map.Entry<String, String> i : params.entrySet()) {
d.append(i.getKey() + "=" + i.getValue() + "&");
}
String sd = new String(d);
URL url = new URL(addr);
HttpURLConnection con = (HttpURLConnection)url.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("User-Agent", "Mozilla/5.0");
con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
con.setDoOutput(true);
con.setDoInput(true);
OutputStream o = con.getOutputStream();
o.write(sd.getBytes("UTF-8"));
o.flush();
o.close();
int responseCode = con.getResponseCode();
System.out.println("\nSending 'POST' request to URL : " + url);
System.out.println("Post parameters : " + addr);
System.out.println("Response Code : " + responseCode);
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
//打印结果
System.out.println(response.toString());
result = response.toString();
} catch (IOException e) {
result = "fail";
e.printStackTrace();
}
finished = true;
}
}
@@ -387,7 +387,7 @@ public class ScheduleFragment extends Fragment implements View.OnClickListener ,
}
//(new SaveViewToImage()).save(schedule);
(new NetWork()).POST("", new HashMap<String, String>());
(new ShareDialog()).show(getActivity());
break;
case R.id.dialogweek:
@@ -0,0 +1,66 @@
package wesayallright.timemanager.surface.scheduleFragment;
import android.app.Activity;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.TextView;
import java.util.HashMap;
import wesayallright.timemanager.InnerLayer.Network.NetWork;
import wesayallright.timemanager.InnerLayer.Network.SendPost;
import wesayallright.timemanager.R;
/**
* Created by mj on 17-5-2.
* 分享对话框
*/
public class ShareDialog {
public void show(Activity a) {
AlertDialog.Builder d = new AlertDialog.Builder(a);
View v = View.inflate(a, R.layout.share_dialog, null);
TextView success = (TextView)v.findViewById(R.id.sd_success);
TextView url = (TextView)v.findViewById(R.id.sd_destination);
HashMap<String, String> data = new HashMap<>();
data.put("user", "super");
data.put("count", "10");
data.put("startAndEnd","7:00/16:00");
data.put("1", "课1/1/7:00/10:00");
data.put("2", "课2/2/7:00/8:00");
data.put("3", "课3/3/8:00/9:30");
data.put("4", "课4/4/7:00/8:00");
data.put("5", "课5/5/11:00/13:00");
data.put("6", "课6/4/10:00/15:00");
data.put("7", "课7/6/7:00/9:00");
data.put("8", "课8/7/7:00/8:00");
data.put("9", "课9/2/10:00/13:00");
data.put("10", "课10/3/10:00/12:00");
SendPost remote = new SendPost();
remote.addPrama(data);
Thread t = new Thread(remote);
t.start();
while (!remote.finished)
;
if (!remote.result.equals("fail")) {
success.setText("成功");
success.setTextColor(0xff00ff0d);
url.setText(remote.result );
}
else {
success.setText("失败");
success.setTextColor(0xffff0000);
url.setText("(╯°Д°)╯︵ /(.□ . \\\\)");
}
d.setView(v);
d.setTitle("分享日程");
d.show();
}
}
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/sd_success"
android:layout_height="wrap_content"
android:layout_width="match_parent" />
<TextView
android:id="@+id/sd_destination"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:autoLink="web"/>
</LinearLayout>
@@ -1,17 +0,0 @@
package wesayallright.timemanager;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
/**
* Example local unit test, which will execute on the development machine (host).
*
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/
public class ExampleUnitTest {
@Test
public void addition_isCorrect() throws Exception {
assertEquals(4, 2 + 2);
}
}