Created
June 20, 2015 22:12
-
-
Save InputOutputZ/e8b760b63fc0a23a97ea to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.zack.test; | |
import android.support.v7.app.ActionBarActivity; | |
import android.os.Bundle; | |
import android.util.Base64; | |
import android.view.Menu; | |
import android.view.MenuItem; | |
import java.io.DataOutputStream; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.InputStreamReader; | |
import java.net.HttpURLConnection; | |
import java.net.MalformedURLException; | |
import java.net.ProtocolException; | |
import java.net.URL; | |
import java.util.zip.DataFormatException; | |
import static android.util.Base64.encode; | |
public class MainActivity extends ActionBarActivity{ | |
private boolean makebase() throws IOException, DataFormatException { | |
URL url = new URL("https://api-eu.clusterpoint.com/1129/hand.json"); | |
HttpURLConnection uc = (HttpURLConnection) url.openConnection(); | |
String userpass = "[email protected]" + ":" + "lostboys"; | |
String basicAuth = "Basic " | |
+ encode(userpass | |
.getBytes(), 0); | |
uc.setRequestProperty("Authorization", basicAuth); | |
uc.setRequestMethod("POST"); | |
uc.setDoOutput(true); | |
// POST data to server | |
// Variables contain POST data for each command | |
String imgebase = ""; | |
String request = "{\"id\" : \""+java.util.UUID.randomUUID().toString()+", name\": \"John\", \"email\" : \"[email protected]\", \"image\" : "+imgebase+", \"extra_info\" : \"extra extra\"}"; | |
DataOutputStream wr = new DataOutputStream(uc.getOutputStream()); | |
wr.writeBytes(request); | |
wr.flush(); | |
wr.close(); | |
// Receive response | |
InputStream in = uc.getInputStream(); | |
InputStreamReader isr = new InputStreamReader(in); | |
int numCharsRead; | |
char[] charArray = new char[1024]; | |
StringBuffer sb = new StringBuffer(); | |
while ((numCharsRead = isr.read(charArray)) > 0) { | |
sb.append(charArray, 0, numCharsRead); | |
} | |
String result = sb.toString(); | |
System.out.println(result); | |
return true; | |
} | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
try { | |
makebase(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} catch (DataFormatException e) { | |
e.printStackTrace(); | |
} | |
} | |
@Override | |
public boolean onCreateOptionsMenu(Menu menu) { | |
// Inflate the menu; this adds items to the action bar if it is present. | |
getMenuInflater().inflate(R.menu.menu_main, menu); | |
return true; | |
} | |
@Override | |
public boolean onOptionsItemSelected(MenuItem item) { | |
// Handle action bar item clicks here. The action bar will | |
// automatically handle clicks on the Home/Up button, so long | |
// as you specify a parent activity in AndroidManifest.xml. | |
int id = item.getItemId(); | |
//noinspection SimplifiableIfStatement | |
if (id == R.id.action_settings) { | |
return true; | |
} | |
return super.onOptionsItemSelected(item); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment