Android Open Source - groundify-android My Http Client






From Project

Back to project page groundify-android.

License

The source code is released under:

GNU General Public License

If you think the Android project groundify-android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package deftsoft.iground.android;
//w  ww  . j  a  v a  2  s  . c  o  m
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import deftsoft.android.iground.utility.HttpUploader;

public class MyHttpClient {

  HttpURLConnection con = null;
  OutputStream os = null;

  HttpUploader uploader;
  private String delimiter = "--";
  private String boundary = "SwA" + Long.toString(System.currentTimeMillis())
      + "SwA";

  // public String getResponse(HttpURLConnection cony) {
  // // TODO Auto-generated method stub
  // StringBuffer buffer = new StringBuffer();
  // InputStream is = null;
  // try {
  // is = con.getInputStream();
  // } catch (IOException e) {
  // // TODO Auto-generated catch block
  // e.printStackTrace();
  // }
  // byte[] b = new byte[1024];
  // try {
  // while (is.read(b) != -1)
  // buffer.append(new String(b));
  // } catch (IOException e) {
  // // TODO Auto-generated catch block
  // e.printStackTrace();
  // }
  // con.disconnect();
  //
  // return buffer.toString();
  // }
  
//  public void uploadFile(String myImagePath) {
//
//    Bitmap myBitmap = BitmapFactory.decodeFile(myImagePath);
//    ByteArrayOutputStream baos = new ByteArrayOutputStream();
//
//    double width = myBitmap.getWidth();
//
//    double height = myBitmap.getHeight();
//
//    double ratio = 400 / width;
//
//    int newheight = (int) (ratio * height);
//
//    myBitmap = Bitmap.createScaledBitmap(myBitmap, 400, newheight, true);
//    myBitmap.compress(Bitmap.CompressFormat.JPEG, 95, baos);
//    try {
//      HttpClient client = new HttpClient();
//      client.connectForMultipart();
//      // client.addFormPart("param1", param1);
//      // client.addFormPart("param2", param2);
//      client.addFilePart("img", "logo.png", baos.toByteArray());
//      client.finishMultipart();
//      String data = client.getResponse();
//    } catch (Throwable t) {
//      t.printStackTrace();
//    }
//
//  }

  public void finishMultipart() throws Exception {
    os.write((delimiter + boundary + delimiter + "\r\n").getBytes());
  }

  public String getResponse() throws Exception {
    InputStream is = con.getInputStream();
    byte[] b1 = new byte[1024];
    StringBuffer buffer = new StringBuffer();

    while (is.read(b1) != -1)
      buffer.append(new String(b1));

    con.disconnect();

    return buffer.toString();
  }

  public HttpURLConnection connectForMultipart() throws Exception {
    con = (HttpURLConnection) (new URL("http://deftsoft.info/familytree//iGroundApp/images/upload.php")).openConnection();
    con.setRequestMethod("POST");
    con.setDoInput(true);
    con.setDoOutput(true);
    con.setRequestProperty("Connection", "Keep-Alive");
    con.setRequestProperty("Content-Type", "multipart/form-data; boundary="
        + boundary);
    con.connect();
    os = con.getOutputStream();
    return con;
  }

  public void addFilePart(String paramName, String fileName, byte[] data)
      throws Exception {
    os.write((delimiter + boundary + "\r\n").getBytes());
    os.write(("Content-Disposition: form-data; name=\"" + paramName
        + "\"; filename=\"" + fileName + "\"\r\n").getBytes());
    os.write(("Content-Type: application/octet-stream\r\n").getBytes());
    os.write(("Content-Transfer-Encoding: binary\r\n").getBytes());
    os.write("\r\n".getBytes());

    os.write(data);

    os.write("\r\n".getBytes());
  }

  public void addFormPart(String paramName, String value) throws Exception {
    writeParamData(paramName, value);
  }

  private void writeParamData(String paramName, String value)
      throws Exception {
    os.write((delimiter + boundary + "\r\n").getBytes());
    os.write("Content-Type: text/plain\r\n".getBytes());
    os.write(("Content-Disposition: form-data; name=\"" + paramName + "\"\r\n")
        .getBytes());

    os.write(("\r\n" + value + "\r\n").getBytes());

  }

}




Java Source Code List

.ConsequenceImageGridActivity.java
custom.adapter.pckg.GridAdapter.java
custom.adapter.pckg.MyAdapter.java
custom.adapter.pckg.MyLazyAdapter.java
custom.adapter.pckg.MySpinnerAdapter.java
deftsoft.android.iground.utility.Base64.java
deftsoft.android.iground.utility.ChildDetail.java
deftsoft.android.iground.utility.DatePickerFragment.java
deftsoft.android.iground.utility.DeleteChildWebResponse.java
deftsoft.android.iground.utility.GetNewCosequence.java
deftsoft.android.iground.utility.HttpUploader.java
deftsoft.android.iground.utility.ImageFilePath.java
deftsoft.android.iground.utility.SetConSeqWebServiceWebResponse.java
deftsoft.android.iground.utility.SignupDetail.java
deftsoft.android.iground.utility.TimePickerFragment.java
deftsoft.android.iground.utility.Utility.java
deftsoft.android.iground.utility.ViewConseqDetail.java
deftsoft.android.iground.utility.WebServiceLinks.java
deftsoft.android.iground.utility.WebServiceResponse.java
deftsoft.android.image.upload.Base64.java
deftsoft.android.image.upload.UploadImageToServer.java
deftsoft.iground.android.AddChildScreen.java
deftsoft.iground.android.AddConsequenceActivity.java
deftsoft.iground.android.BridgeClass.java
deftsoft.iground.android.CopyOfConsequenceImageGridActivity.java
deftsoft.iground.android.DeleteChildScreen.java
deftsoft.iground.android.GrounChild.java
deftsoft.iground.android.HomeScreenActivity.java
deftsoft.iground.android.Home.java
deftsoft.iground.android.LogInScreenActivity.java
deftsoft.iground.android.MyHttpClient.java
deftsoft.iground.android.SignUpScreenActivity.java
deftsoft.iground.android.ThankYouActivity.java
deftsoft.iground.android.UILApplication.java
deftsoft.iground.android.ViewConsequence.java
deftsoft.iground.android.CustomizeListView.AbsListViewBaseActivity.java
deftsoft.iground.android.CustomizeListView.BaseActivity.java
deftsoft.iground.android.CustomizeListView.Constants.java
iground.child.interfaces.Consequnceselected.java